blob: 08bc519ae17f20a231e172889851eb31a74ec49d [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>
Howard Hinnant3e519522010-05-11 19:42:16 +000021
Howard Hinnantab4f4382011-11-29 16:45:27 +000022#include <__undef_min_max>
Saleem Abdulrasool8e5ce332015-02-13 22:15:32 +000023#include <__undef___deallocate>
Howard Hinnantab4f4382011-11-29 16:45:27 +000024
Eric Fiselierc1bd9192014-08-10 23:53:08 +000025#include <__debug>
Howard Hinnant42a30462013-08-02 00:26:35 +000026
Howard Hinnant073458b2011-10-17 20:05:10 +000027#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnant3e519522010-05-11 19:42:16 +000028#pragma GCC system_header
Howard Hinnant073458b2011-10-17 20:05:10 +000029#endif
Howard Hinnant3e519522010-05-11 19:42:16 +000030
31_LIBCPP_BEGIN_NAMESPACE_STD
32
Eric Fiselierfcd02212016-02-11 11:59:44 +000033
34#ifndef _LIBCPP_CXX03_LANG
35template <class _Key, class _Tp>
36union __hash_value_type;
37#else
38template <class _Key, class _Tp>
39struct __hash_value_type;
40#endif
41
42#ifndef _LIBCPP_CXX03_LANG
43template <class _Tp>
44struct __is_hash_value_type_imp : false_type {};
45
46template <class _Key, class _Value>
47struct __is_hash_value_type_imp<__hash_value_type<_Key, _Value>> : true_type {};
48
49template <class ..._Args>
50struct __is_hash_value_type : false_type {};
51
52template <class _One>
53struct __is_hash_value_type<_One> : __is_hash_value_type_imp<typename __uncvref<_One>::type> {};
54#endif
55
Howard Hinnant6e412562013-03-06 23:30:19 +000056_LIBCPP_FUNC_VIS
Howard Hinnantce534202011-06-14 19:58:17 +000057size_t __next_prime(size_t __n);
Howard Hinnant3e519522010-05-11 19:42:16 +000058
59template <class _NodePtr>
60struct __hash_node_base
61{
62 typedef __hash_node_base __first_node;
Howard Hinnant3e519522010-05-11 19:42:16 +000063
Howard Hinnant73736ef2011-02-27 18:02:02 +000064 _NodePtr __next_;
Howard Hinnant3e519522010-05-11 19:42:16 +000065
Howard Hinnant37141072011-06-04 18:54:24 +000066 _LIBCPP_INLINE_VISIBILITY __hash_node_base() _NOEXCEPT : __next_(nullptr) {}
Howard Hinnant3e519522010-05-11 19:42:16 +000067};
68
69template <class _Tp, class _VoidPtr>
70struct __hash_node
71 : public __hash_node_base
72 <
Eric Fiselier934b0922015-12-30 21:52:00 +000073 typename __rebind_pointer<_VoidPtr, __hash_node<_Tp, _VoidPtr> >::type
Howard Hinnant3e519522010-05-11 19:42:16 +000074 >
75{
Eric Fiselier75d0dcf2016-02-10 20:46:23 +000076 typedef _Tp __node_value_type;
Howard Hinnant3e519522010-05-11 19:42:16 +000077
78 size_t __hash_;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +000079 __node_value_type __value_;
Howard Hinnant3e519522010-05-11 19:42:16 +000080};
81
Howard Hinnant4cb38a82012-07-06 17:31:14 +000082inline _LIBCPP_INLINE_VISIBILITY
83bool
Eric Fiselier9ba5c112015-02-02 21:31:48 +000084__is_hash_power2(size_t __bc)
Howard Hinnant4cb38a82012-07-06 17:31:14 +000085{
86 return __bc > 2 && !(__bc & (__bc - 1));
87}
88
89inline _LIBCPP_INLINE_VISIBILITY
90size_t
91__constrain_hash(size_t __h, size_t __bc)
92{
Eric Fiselier118cb412016-07-11 22:02:02 +000093 return !(__bc & (__bc - 1)) ? __h & (__bc - 1) :
94 (__h < __bc ? __h : __h % __bc);
Howard Hinnant4cb38a82012-07-06 17:31:14 +000095}
96
97inline _LIBCPP_INLINE_VISIBILITY
98size_t
Eric Fiselier9ba5c112015-02-02 21:31:48 +000099__next_hash_pow2(size_t __n)
Howard Hinnant4cb38a82012-07-06 17:31:14 +0000100{
101 return size_t(1) << (std::numeric_limits<size_t>::digits - __clz(__n-1));
102}
103
Duncan P. N. Exon Smithfde79b42016-03-17 20:45:20 +0000104
Howard Hinnantce534202011-06-14 19:58:17 +0000105template <class _Tp, class _Hash, class _Equal, class _Alloc> class __hash_table;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000106
107template <class _NodePtr> class _LIBCPP_TYPE_VIS_ONLY __hash_iterator;
Howard Hinnantf0544c22013-08-12 18:38:34 +0000108template <class _ConstNodePtr> class _LIBCPP_TYPE_VIS_ONLY __hash_const_iterator;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000109template <class _NodePtr> class _LIBCPP_TYPE_VIS_ONLY __hash_local_iterator;
110template <class _ConstNodePtr> class _LIBCPP_TYPE_VIS_ONLY __hash_const_local_iterator;
Howard Hinnantf0544c22013-08-12 18:38:34 +0000111template <class _HashIterator> class _LIBCPP_TYPE_VIS_ONLY __hash_map_iterator;
112template <class _HashIterator> class _LIBCPP_TYPE_VIS_ONLY __hash_map_const_iterator;
Howard Hinnant3e519522010-05-11 19:42:16 +0000113
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000114template <class _Tp>
Eric Fiselier43b121d2016-02-20 07:59:16 +0000115struct __hash_key_value_types {
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000116 static_assert(!is_reference<_Tp>::value && !is_const<_Tp>::value, "");
117 typedef _Tp key_type;
118 typedef _Tp __node_value_type;
119 typedef _Tp __container_value_type;
120 static const bool __is_map = false;
Eric Fiselierfcd02212016-02-11 11:59:44 +0000121
122 _LIBCPP_INLINE_VISIBILITY
123 static key_type const& __get_key(_Tp const& __v) {
124 return __v;
125 }
126 _LIBCPP_INLINE_VISIBILITY
127 static __container_value_type const& __get_value(__node_value_type const& __v) {
128 return __v;
129 }
130 _LIBCPP_INLINE_VISIBILITY
131 static __container_value_type* __get_ptr(__node_value_type& __n) {
132 return _VSTD::addressof(__n);
133 }
134#ifndef _LIBCPP_CXX03_LANG
135 _LIBCPP_INLINE_VISIBILITY
136 static __container_value_type&& __move(__node_value_type& __v) {
137 return _VSTD::move(__v);
138 }
139#endif
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000140};
141
142template <class _Key, class _Tp>
Eric Fiselier43b121d2016-02-20 07:59:16 +0000143struct __hash_key_value_types<__hash_value_type<_Key, _Tp> > {
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000144 typedef _Key key_type;
145 typedef _Tp mapped_type;
146 typedef __hash_value_type<_Key, _Tp> __node_value_type;
147 typedef pair<const _Key, _Tp> __container_value_type;
Eric Fiselierfcd02212016-02-11 11:59:44 +0000148 typedef pair<_Key, _Tp> __nc_value_type;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000149 typedef __container_value_type __map_value_type;
150 static const bool __is_map = true;
Eric Fiselierfcd02212016-02-11 11:59:44 +0000151
152 _LIBCPP_INLINE_VISIBILITY
153 static key_type const& __get_key(__container_value_type const& __v) {
154 return __v.first;
155 }
156
157 template <class _Up>
158 _LIBCPP_INLINE_VISIBILITY
159 static typename enable_if<__is_same_uncvref<_Up, __node_value_type>::value,
160 __container_value_type const&>::type
161 __get_value(_Up& __t) {
162 return __t.__cc;
163 }
164
165 template <class _Up>
166 _LIBCPP_INLINE_VISIBILITY
167 static typename enable_if<__is_same_uncvref<_Up, __container_value_type>::value,
168 __container_value_type const&>::type
169 __get_value(_Up& __t) {
170 return __t;
171 }
172
173 _LIBCPP_INLINE_VISIBILITY
174 static __container_value_type* __get_ptr(__node_value_type& __n) {
175 return _VSTD::addressof(__n.__cc);
176 }
177#ifndef _LIBCPP_CXX03_LANG
178 _LIBCPP_INLINE_VISIBILITY
179 static __nc_value_type&& __move(__node_value_type& __v) {
180 return _VSTD::move(__v.__nc);
181 }
182#endif
183
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000184};
185
Eric Fiselier43b121d2016-02-20 07:59:16 +0000186template <class _Tp, class _AllocPtr, class _KVTypes = __hash_key_value_types<_Tp>,
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000187 bool = _KVTypes::__is_map>
Eric Fiselier43b121d2016-02-20 07:59:16 +0000188struct __hash_map_pointer_types {};
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000189
190template <class _Tp, class _AllocPtr, class _KVTypes>
Eric Fiselier43b121d2016-02-20 07:59:16 +0000191struct __hash_map_pointer_types<_Tp, _AllocPtr, _KVTypes, true> {
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000192 typedef typename _KVTypes::__map_value_type _Mv;
193 typedef typename __rebind_pointer<_AllocPtr, _Mv>::type
194 __map_value_type_pointer;
195 typedef typename __rebind_pointer<_AllocPtr, const _Mv>::type
196 __const_map_value_type_pointer;
197};
198
199template <class _NodePtr, class _NodeT = typename pointer_traits<_NodePtr>::element_type>
200struct __hash_node_types;
201
202template <class _NodePtr, class _Tp, class _VoidPtr>
203struct __hash_node_types<_NodePtr, __hash_node<_Tp, _VoidPtr> >
Eric Fiselier43b121d2016-02-20 07:59:16 +0000204 : public __hash_key_value_types<_Tp>, __hash_map_pointer_types<_Tp, _VoidPtr>
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000205
206{
Eric Fiselier43b121d2016-02-20 07:59:16 +0000207 typedef __hash_key_value_types<_Tp> __base;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000208
209public:
210 typedef ptrdiff_t difference_type;
211 typedef size_t size_type;
212
213 typedef typename __rebind_pointer<_NodePtr, void>::type __void_pointer;
214
215 typedef typename pointer_traits<_NodePtr>::element_type __node_type;
216 typedef _NodePtr __node_pointer;
217
218 typedef __hash_node_base<__node_pointer> __node_base_type;
219 typedef typename __rebind_pointer<_NodePtr, __node_base_type>::type
220 __node_base_pointer;
221
222 typedef _Tp __node_value_type;
223 typedef typename __rebind_pointer<_VoidPtr, __node_value_type>::type
224 __node_value_type_pointer;
225 typedef typename __rebind_pointer<_VoidPtr, const __node_value_type>::type
226 __const_node_value_type_pointer;
227private:
228 static_assert(!is_const<__node_type>::value,
229 "_NodePtr should never be a pointer to const");
230 static_assert((is_same<typename pointer_traits<_VoidPtr>::element_type, void>::value),
231 "_VoidPtr does not point to unqualified void type");
232 static_assert((is_same<typename __rebind_pointer<_VoidPtr, __node_type>::type,
233 _NodePtr>::value), "_VoidPtr does not rebind to _NodePtr.");
234};
235
236
237
238template <class _HashIterator>
239struct __hash_node_types_from_iterator;
240template <class _NodePtr>
241struct __hash_node_types_from_iterator<__hash_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
242template <class _NodePtr>
243struct __hash_node_types_from_iterator<__hash_const_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
244template <class _NodePtr>
245struct __hash_node_types_from_iterator<__hash_local_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
246template <class _NodePtr>
247struct __hash_node_types_from_iterator<__hash_const_local_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
248
249
250template <class _NodeValueTp, class _VoidPtr>
251struct __make_hash_node_types {
252 typedef __hash_node<_NodeValueTp, _VoidPtr> _NodeTp;
253 typedef typename __rebind_pointer<_VoidPtr, _NodeTp>::type _NodePtr;
254 typedef __hash_node_types<_NodePtr> type;
255};
256
Howard Hinnant3e519522010-05-11 19:42:16 +0000257template <class _NodePtr>
Howard Hinnantf0544c22013-08-12 18:38:34 +0000258class _LIBCPP_TYPE_VIS_ONLY __hash_iterator
Howard Hinnant3e519522010-05-11 19:42:16 +0000259{
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000260 typedef __hash_node_types<_NodePtr> _NodeTypes;
261 typedef _NodePtr __node_pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000262
263 __node_pointer __node_;
264
265public:
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000266 typedef forward_iterator_tag iterator_category;
267 typedef typename _NodeTypes::__node_value_type value_type;
268 typedef typename _NodeTypes::difference_type difference_type;
269 typedef value_type& reference;
270 typedef typename _NodeTypes::__node_value_type_pointer pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000271
Howard Hinnantb24c8022013-07-23 22:01:58 +0000272 _LIBCPP_INLINE_VISIBILITY __hash_iterator() _NOEXCEPT
Marshall Clow2c1a8942013-08-07 21:30:44 +0000273#if _LIBCPP_STD_VER > 11
274 : __node_(nullptr)
275#endif
Howard Hinnantb24c8022013-07-23 22:01:58 +0000276 {
277#if _LIBCPP_DEBUG_LEVEL >= 2
278 __get_db()->__insert_i(this);
279#endif
280 }
281
282#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant3e519522010-05-11 19:42:16 +0000283
Howard Hinnant43d99232010-09-21 17:32:39 +0000284 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb24c8022013-07-23 22:01:58 +0000285 __hash_iterator(const __hash_iterator& __i)
286 : __node_(__i.__node_)
287 {
288 __get_db()->__iterator_copy(this, &__i);
289 }
290
Howard Hinnant43d99232010-09-21 17:32:39 +0000291 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb24c8022013-07-23 22:01:58 +0000292 ~__hash_iterator()
293 {
294 __get_db()->__erase_i(this);
295 }
296
297 _LIBCPP_INLINE_VISIBILITY
298 __hash_iterator& operator=(const __hash_iterator& __i)
299 {
300 if (this != &__i)
301 {
302 __get_db()->__iterator_copy(this, &__i);
303 __node_ = __i.__node_;
304 }
305 return *this;
306 }
307
308#endif // _LIBCPP_DEBUG_LEVEL >= 2
309
310 _LIBCPP_INLINE_VISIBILITY
311 reference operator*() const
312 {
313#if _LIBCPP_DEBUG_LEVEL >= 2
314 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
315 "Attempted to dereference a non-dereferenceable unordered container iterator");
316#endif
317 return __node_->__value_;
318 }
319 _LIBCPP_INLINE_VISIBILITY
320 pointer operator->() const
321 {
322#if _LIBCPP_DEBUG_LEVEL >= 2
323 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
324 "Attempted to dereference a non-dereferenceable unordered container iterator");
325#endif
326 return pointer_traits<pointer>::pointer_to(__node_->__value_);
327 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000328
Howard Hinnant43d99232010-09-21 17:32:39 +0000329 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000330 __hash_iterator& operator++()
331 {
Howard Hinnantb24c8022013-07-23 22:01:58 +0000332#if _LIBCPP_DEBUG_LEVEL >= 2
333 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
334 "Attempted to increment non-incrementable unordered container iterator");
335#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000336 __node_ = __node_->__next_;
337 return *this;
338 }
339
Howard Hinnant43d99232010-09-21 17:32:39 +0000340 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000341 __hash_iterator operator++(int)
342 {
343 __hash_iterator __t(*this);
344 ++(*this);
345 return __t;
346 }
347
Howard Hinnant43d99232010-09-21 17:32:39 +0000348 friend _LIBCPP_INLINE_VISIBILITY
349 bool operator==(const __hash_iterator& __x, const __hash_iterator& __y)
Howard Hinnantb24c8022013-07-23 22:01:58 +0000350 {
Howard Hinnantb24c8022013-07-23 22:01:58 +0000351 return __x.__node_ == __y.__node_;
352 }
Howard Hinnant43d99232010-09-21 17:32:39 +0000353 friend _LIBCPP_INLINE_VISIBILITY
354 bool operator!=(const __hash_iterator& __x, const __hash_iterator& __y)
Howard Hinnantb24c8022013-07-23 22:01:58 +0000355 {return !(__x == __y);}
Howard Hinnant3e519522010-05-11 19:42:16 +0000356
357private:
Howard Hinnantb24c8022013-07-23 22:01:58 +0000358#if _LIBCPP_DEBUG_LEVEL >= 2
359 _LIBCPP_INLINE_VISIBILITY
360 __hash_iterator(__node_pointer __node, const void* __c) _NOEXCEPT
361 : __node_(__node)
362 {
363 __get_db()->__insert_ic(this, __c);
364 }
365#else
Howard Hinnant43d99232010-09-21 17:32:39 +0000366 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +0000367 __hash_iterator(__node_pointer __node) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000368 : __node_(__node)
369 {}
Howard Hinnantb24c8022013-07-23 22:01:58 +0000370#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000371
372 template <class, class, class, class> friend class __hash_table;
Howard Hinnantf0544c22013-08-12 18:38:34 +0000373 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_const_iterator;
374 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_map_iterator;
375 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_map;
376 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_multimap;
Howard Hinnant3e519522010-05-11 19:42:16 +0000377};
378
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000379template <class _NodePtr>
Howard Hinnantf0544c22013-08-12 18:38:34 +0000380class _LIBCPP_TYPE_VIS_ONLY __hash_const_iterator
Howard Hinnant3e519522010-05-11 19:42:16 +0000381{
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000382 static_assert(!is_const<typename pointer_traits<_NodePtr>::element_type>::value, "");
383 typedef __hash_node_types<_NodePtr> _NodeTypes;
384 typedef _NodePtr __node_pointer;
Eric Fiselier757373e2016-02-18 00:20:34 +0000385
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000386 __node_pointer __node_;
Howard Hinnant3e519522010-05-11 19:42:16 +0000387
388public:
Eric Fiselier757373e2016-02-18 00:20:34 +0000389 typedef __hash_iterator<_NodePtr> __non_const_iterator;
390
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000391 typedef forward_iterator_tag iterator_category;
392 typedef typename _NodeTypes::__node_value_type value_type;
393 typedef typename _NodeTypes::difference_type difference_type;
394 typedef const value_type& reference;
395 typedef typename _NodeTypes::__const_node_value_type_pointer pointer;
396
Howard Hinnant3e519522010-05-11 19:42:16 +0000397
Howard Hinnantb24c8022013-07-23 22:01:58 +0000398 _LIBCPP_INLINE_VISIBILITY __hash_const_iterator() _NOEXCEPT
Marshall Clow2c1a8942013-08-07 21:30:44 +0000399#if _LIBCPP_STD_VER > 11
400 : __node_(nullptr)
401#endif
Howard Hinnantb24c8022013-07-23 22:01:58 +0000402 {
403#if _LIBCPP_DEBUG_LEVEL >= 2
404 __get_db()->__insert_i(this);
405#endif
406 }
Howard Hinnant43d99232010-09-21 17:32:39 +0000407 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +0000408 __hash_const_iterator(const __non_const_iterator& __x) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000409 : __node_(__x.__node_)
Howard Hinnantb24c8022013-07-23 22:01:58 +0000410 {
411#if _LIBCPP_DEBUG_LEVEL >= 2
412 __get_db()->__iterator_copy(this, &__x);
413#endif
414 }
415
416#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant3e519522010-05-11 19:42:16 +0000417
Howard Hinnant43d99232010-09-21 17:32:39 +0000418 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb24c8022013-07-23 22:01:58 +0000419 __hash_const_iterator(const __hash_const_iterator& __i)
420 : __node_(__i.__node_)
421 {
422 __get_db()->__iterator_copy(this, &__i);
423 }
424
Howard Hinnant43d99232010-09-21 17:32:39 +0000425 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb24c8022013-07-23 22:01:58 +0000426 ~__hash_const_iterator()
427 {
428 __get_db()->__erase_i(this);
429 }
430
431 _LIBCPP_INLINE_VISIBILITY
432 __hash_const_iterator& operator=(const __hash_const_iterator& __i)
433 {
434 if (this != &__i)
435 {
436 __get_db()->__iterator_copy(this, &__i);
437 __node_ = __i.__node_;
438 }
439 return *this;
440 }
441
442#endif // _LIBCPP_DEBUG_LEVEL >= 2
443
444 _LIBCPP_INLINE_VISIBILITY
445 reference operator*() const
446 {
447#if _LIBCPP_DEBUG_LEVEL >= 2
448 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
449 "Attempted to dereference a non-dereferenceable unordered container const_iterator");
450#endif
451 return __node_->__value_;
452 }
453 _LIBCPP_INLINE_VISIBILITY
454 pointer operator->() const
455 {
456#if _LIBCPP_DEBUG_LEVEL >= 2
457 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
458 "Attempted to dereference a non-dereferenceable unordered container const_iterator");
459#endif
460 return pointer_traits<pointer>::pointer_to(__node_->__value_);
461 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000462
Howard Hinnant43d99232010-09-21 17:32:39 +0000463 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000464 __hash_const_iterator& operator++()
465 {
Howard Hinnantb24c8022013-07-23 22:01:58 +0000466#if _LIBCPP_DEBUG_LEVEL >= 2
467 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
468 "Attempted to increment non-incrementable unordered container const_iterator");
469#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000470 __node_ = __node_->__next_;
471 return *this;
472 }
473
Howard Hinnant43d99232010-09-21 17:32:39 +0000474 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000475 __hash_const_iterator operator++(int)
476 {
477 __hash_const_iterator __t(*this);
478 ++(*this);
479 return __t;
480 }
481
Howard Hinnant43d99232010-09-21 17:32:39 +0000482 friend _LIBCPP_INLINE_VISIBILITY
483 bool operator==(const __hash_const_iterator& __x, const __hash_const_iterator& __y)
Howard Hinnantb24c8022013-07-23 22:01:58 +0000484 {
Howard Hinnantb24c8022013-07-23 22:01:58 +0000485 return __x.__node_ == __y.__node_;
486 }
Howard Hinnant43d99232010-09-21 17:32:39 +0000487 friend _LIBCPP_INLINE_VISIBILITY
488 bool operator!=(const __hash_const_iterator& __x, const __hash_const_iterator& __y)
Howard Hinnantb24c8022013-07-23 22:01:58 +0000489 {return !(__x == __y);}
Howard Hinnant3e519522010-05-11 19:42:16 +0000490
491private:
Howard Hinnantb24c8022013-07-23 22:01:58 +0000492#if _LIBCPP_DEBUG_LEVEL >= 2
493 _LIBCPP_INLINE_VISIBILITY
494 __hash_const_iterator(__node_pointer __node, const void* __c) _NOEXCEPT
495 : __node_(__node)
496 {
497 __get_db()->__insert_ic(this, __c);
498 }
499#else
Howard Hinnant43d99232010-09-21 17:32:39 +0000500 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +0000501 __hash_const_iterator(__node_pointer __node) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000502 : __node_(__node)
503 {}
Howard Hinnantb24c8022013-07-23 22:01:58 +0000504#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000505
506 template <class, class, class, class> friend class __hash_table;
Howard Hinnantf0544c22013-08-12 18:38:34 +0000507 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_map_const_iterator;
508 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_map;
509 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_multimap;
Howard Hinnant3e519522010-05-11 19:42:16 +0000510};
511
Howard Hinnant3e519522010-05-11 19:42:16 +0000512template <class _NodePtr>
Howard Hinnantf0544c22013-08-12 18:38:34 +0000513class _LIBCPP_TYPE_VIS_ONLY __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;
516 typedef _NodePtr __node_pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000517
518 __node_pointer __node_;
519 size_t __bucket_;
520 size_t __bucket_count_;
521
Howard Hinnant3e519522010-05-11 19:42:16 +0000522public:
523 typedef forward_iterator_tag iterator_category;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000524 typedef typename _NodeTypes::__node_value_type value_type;
525 typedef typename _NodeTypes::difference_type difference_type;
Howard Hinnant3e519522010-05-11 19:42:16 +0000526 typedef value_type& reference;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000527 typedef typename _NodeTypes::__node_value_type_pointer pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000528
Howard Hinnantb24c8022013-07-23 22:01:58 +0000529 _LIBCPP_INLINE_VISIBILITY __hash_local_iterator() _NOEXCEPT
530 {
531#if _LIBCPP_DEBUG_LEVEL >= 2
532 __get_db()->__insert_i(this);
533#endif
534 }
535
536#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant3e519522010-05-11 19:42:16 +0000537
Howard Hinnant43d99232010-09-21 17:32:39 +0000538 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb24c8022013-07-23 22:01:58 +0000539 __hash_local_iterator(const __hash_local_iterator& __i)
540 : __node_(__i.__node_),
541 __bucket_(__i.__bucket_),
542 __bucket_count_(__i.__bucket_count_)
543 {
544 __get_db()->__iterator_copy(this, &__i);
545 }
546
Howard Hinnant43d99232010-09-21 17:32:39 +0000547 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb24c8022013-07-23 22:01:58 +0000548 ~__hash_local_iterator()
549 {
550 __get_db()->__erase_i(this);
551 }
552
553 _LIBCPP_INLINE_VISIBILITY
554 __hash_local_iterator& operator=(const __hash_local_iterator& __i)
555 {
556 if (this != &__i)
557 {
558 __get_db()->__iterator_copy(this, &__i);
559 __node_ = __i.__node_;
560 __bucket_ = __i.__bucket_;
561 __bucket_count_ = __i.__bucket_count_;
562 }
563 return *this;
564 }
565
566#endif // _LIBCPP_DEBUG_LEVEL >= 2
567
568 _LIBCPP_INLINE_VISIBILITY
569 reference operator*() const
570 {
571#if _LIBCPP_DEBUG_LEVEL >= 2
572 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
573 "Attempted to dereference a non-dereferenceable unordered container local_iterator");
574#endif
575 return __node_->__value_;
576 }
577 _LIBCPP_INLINE_VISIBILITY
578 pointer operator->() const
579 {
580#if _LIBCPP_DEBUG_LEVEL >= 2
581 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
582 "Attempted to dereference a non-dereferenceable unordered container local_iterator");
583#endif
584 return pointer_traits<pointer>::pointer_to(__node_->__value_);
585 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000586
Howard Hinnant43d99232010-09-21 17:32:39 +0000587 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000588 __hash_local_iterator& operator++()
589 {
Howard Hinnantb24c8022013-07-23 22:01:58 +0000590#if _LIBCPP_DEBUG_LEVEL >= 2
591 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
592 "Attempted to increment non-incrementable unordered container local_iterator");
593#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000594 __node_ = __node_->__next_;
Howard Hinnant4cb38a82012-07-06 17:31:14 +0000595 if (__node_ != nullptr && __constrain_hash(__node_->__hash_, __bucket_count_) != __bucket_)
Howard Hinnant3e519522010-05-11 19:42:16 +0000596 __node_ = nullptr;
597 return *this;
598 }
599
Howard Hinnant43d99232010-09-21 17:32:39 +0000600 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000601 __hash_local_iterator operator++(int)
602 {
603 __hash_local_iterator __t(*this);
604 ++(*this);
605 return __t;
606 }
607
Howard Hinnant43d99232010-09-21 17:32:39 +0000608 friend _LIBCPP_INLINE_VISIBILITY
609 bool operator==(const __hash_local_iterator& __x, const __hash_local_iterator& __y)
Howard Hinnantb24c8022013-07-23 22:01:58 +0000610 {
Howard Hinnantb24c8022013-07-23 22:01:58 +0000611 return __x.__node_ == __y.__node_;
612 }
Howard Hinnant43d99232010-09-21 17:32:39 +0000613 friend _LIBCPP_INLINE_VISIBILITY
614 bool operator!=(const __hash_local_iterator& __x, const __hash_local_iterator& __y)
Howard Hinnantb24c8022013-07-23 22:01:58 +0000615 {return !(__x == __y);}
Howard Hinnant3e519522010-05-11 19:42:16 +0000616
617private:
Howard Hinnantb24c8022013-07-23 22:01:58 +0000618#if _LIBCPP_DEBUG_LEVEL >= 2
619 _LIBCPP_INLINE_VISIBILITY
620 __hash_local_iterator(__node_pointer __node, size_t __bucket,
621 size_t __bucket_count, const void* __c) _NOEXCEPT
622 : __node_(__node),
623 __bucket_(__bucket),
624 __bucket_count_(__bucket_count)
625 {
626 __get_db()->__insert_ic(this, __c);
627 if (__node_ != nullptr)
628 __node_ = __node_->__next_;
629 }
630#else
Howard Hinnant43d99232010-09-21 17:32:39 +0000631 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000632 __hash_local_iterator(__node_pointer __node, size_t __bucket,
Howard Hinnant37141072011-06-04 18:54:24 +0000633 size_t __bucket_count) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000634 : __node_(__node),
635 __bucket_(__bucket),
636 __bucket_count_(__bucket_count)
637 {
638 if (__node_ != nullptr)
639 __node_ = __node_->__next_;
640 }
Howard Hinnantb24c8022013-07-23 22:01:58 +0000641#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000642 template <class, class, class, class> friend class __hash_table;
Howard Hinnantf0544c22013-08-12 18:38:34 +0000643 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_const_local_iterator;
644 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_map_iterator;
Howard Hinnant3e519522010-05-11 19:42:16 +0000645};
646
647template <class _ConstNodePtr>
Howard Hinnantf0544c22013-08-12 18:38:34 +0000648class _LIBCPP_TYPE_VIS_ONLY __hash_const_local_iterator
Howard Hinnant3e519522010-05-11 19:42:16 +0000649{
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000650 typedef __hash_node_types<_ConstNodePtr> _NodeTypes;
651 typedef _ConstNodePtr __node_pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000652
653 __node_pointer __node_;
654 size_t __bucket_;
655 size_t __bucket_count_;
656
657 typedef pointer_traits<__node_pointer> __pointer_traits;
658 typedef typename __pointer_traits::element_type __node;
659 typedef typename remove_const<__node>::type __non_const_node;
Eric Fiselier934b0922015-12-30 21:52:00 +0000660 typedef typename __rebind_pointer<__node_pointer, __non_const_node>::type
661 __non_const_node_pointer;
Eric Fiselier757373e2016-02-18 00:20:34 +0000662public:
Howard Hinnant3e519522010-05-11 19:42:16 +0000663 typedef __hash_local_iterator<__non_const_node_pointer>
664 __non_const_iterator;
Eric Fiselier757373e2016-02-18 00:20:34 +0000665
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000666 typedef forward_iterator_tag iterator_category;
667 typedef typename _NodeTypes::__node_value_type value_type;
668 typedef typename _NodeTypes::difference_type difference_type;
669 typedef const value_type& reference;
670 typedef typename _NodeTypes::__const_node_value_type_pointer pointer;
Eric Fiselier934b0922015-12-30 21:52:00 +0000671
Howard Hinnant3e519522010-05-11 19:42:16 +0000672
Howard Hinnantb24c8022013-07-23 22:01:58 +0000673 _LIBCPP_INLINE_VISIBILITY __hash_const_local_iterator() _NOEXCEPT
674 {
675#if _LIBCPP_DEBUG_LEVEL >= 2
676 __get_db()->__insert_i(this);
677#endif
678 }
679
Howard Hinnant43d99232010-09-21 17:32:39 +0000680 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +0000681 __hash_const_local_iterator(const __non_const_iterator& __x) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000682 : __node_(__x.__node_),
683 __bucket_(__x.__bucket_),
684 __bucket_count_(__x.__bucket_count_)
Howard Hinnantb24c8022013-07-23 22:01:58 +0000685 {
686#if _LIBCPP_DEBUG_LEVEL >= 2
687 __get_db()->__iterator_copy(this, &__x);
688#endif
689 }
690
691#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant3e519522010-05-11 19:42:16 +0000692
Howard Hinnant43d99232010-09-21 17:32:39 +0000693 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb24c8022013-07-23 22:01:58 +0000694 __hash_const_local_iterator(const __hash_const_local_iterator& __i)
695 : __node_(__i.__node_),
696 __bucket_(__i.__bucket_),
697 __bucket_count_(__i.__bucket_count_)
698 {
699 __get_db()->__iterator_copy(this, &__i);
700 }
701
Howard Hinnant43d99232010-09-21 17:32:39 +0000702 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb24c8022013-07-23 22:01:58 +0000703 ~__hash_const_local_iterator()
704 {
705 __get_db()->__erase_i(this);
706 }
707
708 _LIBCPP_INLINE_VISIBILITY
709 __hash_const_local_iterator& operator=(const __hash_const_local_iterator& __i)
710 {
711 if (this != &__i)
712 {
713 __get_db()->__iterator_copy(this, &__i);
714 __node_ = __i.__node_;
715 __bucket_ = __i.__bucket_;
716 __bucket_count_ = __i.__bucket_count_;
717 }
718 return *this;
719 }
720
721#endif // _LIBCPP_DEBUG_LEVEL >= 2
722
723 _LIBCPP_INLINE_VISIBILITY
724 reference operator*() const
725 {
726#if _LIBCPP_DEBUG_LEVEL >= 2
727 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
728 "Attempted to dereference a non-dereferenceable unordered container const_local_iterator");
729#endif
730 return __node_->__value_;
731 }
732 _LIBCPP_INLINE_VISIBILITY
733 pointer operator->() const
734 {
735#if _LIBCPP_DEBUG_LEVEL >= 2
736 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
737 "Attempted to dereference a non-dereferenceable unordered container const_local_iterator");
738#endif
739 return pointer_traits<pointer>::pointer_to(__node_->__value_);
740 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000741
Howard Hinnant43d99232010-09-21 17:32:39 +0000742 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000743 __hash_const_local_iterator& operator++()
744 {
Howard Hinnantb24c8022013-07-23 22:01:58 +0000745#if _LIBCPP_DEBUG_LEVEL >= 2
746 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
747 "Attempted to increment non-incrementable unordered container const_local_iterator");
748#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000749 __node_ = __node_->__next_;
Howard Hinnant4cb38a82012-07-06 17:31:14 +0000750 if (__node_ != nullptr && __constrain_hash(__node_->__hash_, __bucket_count_) != __bucket_)
Howard Hinnant3e519522010-05-11 19:42:16 +0000751 __node_ = nullptr;
752 return *this;
753 }
754
Howard Hinnant43d99232010-09-21 17:32:39 +0000755 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000756 __hash_const_local_iterator operator++(int)
757 {
758 __hash_const_local_iterator __t(*this);
759 ++(*this);
760 return __t;
761 }
762
Howard Hinnant43d99232010-09-21 17:32:39 +0000763 friend _LIBCPP_INLINE_VISIBILITY
764 bool operator==(const __hash_const_local_iterator& __x, const __hash_const_local_iterator& __y)
Howard Hinnantb24c8022013-07-23 22:01:58 +0000765 {
Howard Hinnantb24c8022013-07-23 22:01:58 +0000766 return __x.__node_ == __y.__node_;
767 }
Howard Hinnant43d99232010-09-21 17:32:39 +0000768 friend _LIBCPP_INLINE_VISIBILITY
769 bool operator!=(const __hash_const_local_iterator& __x, const __hash_const_local_iterator& __y)
Howard Hinnantb24c8022013-07-23 22:01:58 +0000770 {return !(__x == __y);}
Howard Hinnant3e519522010-05-11 19:42:16 +0000771
772private:
Howard Hinnantb24c8022013-07-23 22:01:58 +0000773#if _LIBCPP_DEBUG_LEVEL >= 2
774 _LIBCPP_INLINE_VISIBILITY
775 __hash_const_local_iterator(__node_pointer __node, size_t __bucket,
776 size_t __bucket_count, const void* __c) _NOEXCEPT
777 : __node_(__node),
778 __bucket_(__bucket),
779 __bucket_count_(__bucket_count)
780 {
781 __get_db()->__insert_ic(this, __c);
782 if (__node_ != nullptr)
783 __node_ = __node_->__next_;
784 }
785#else
Howard Hinnant43d99232010-09-21 17:32:39 +0000786 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000787 __hash_const_local_iterator(__node_pointer __node, size_t __bucket,
Howard Hinnant37141072011-06-04 18:54:24 +0000788 size_t __bucket_count) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000789 : __node_(__node),
790 __bucket_(__bucket),
791 __bucket_count_(__bucket_count)
792 {
793 if (__node_ != nullptr)
794 __node_ = __node_->__next_;
795 }
Howard Hinnantb24c8022013-07-23 22:01:58 +0000796#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000797 template <class, class, class, class> friend class __hash_table;
Howard Hinnantf0544c22013-08-12 18:38:34 +0000798 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_map_const_iterator;
Howard Hinnant3e519522010-05-11 19:42:16 +0000799};
800
801template <class _Alloc>
802class __bucket_list_deallocator
803{
804 typedef _Alloc allocator_type;
805 typedef allocator_traits<allocator_type> __alloc_traits;
806 typedef typename __alloc_traits::size_type size_type;
807
808 __compressed_pair<size_type, allocator_type> __data_;
809public:
810 typedef typename __alloc_traits::pointer pointer;
811
Howard Hinnant43d99232010-09-21 17:32:39 +0000812 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000813 __bucket_list_deallocator()
Howard Hinnant37141072011-06-04 18:54:24 +0000814 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +0000815 : __data_(0) {}
Howard Hinnant43d99232010-09-21 17:32:39 +0000816
817 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000818 __bucket_list_deallocator(const allocator_type& __a, size_type __size)
Howard Hinnant37141072011-06-04 18:54:24 +0000819 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +0000820 : __data_(__size, __a) {}
821
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000822#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +0000823
Howard Hinnant43d99232010-09-21 17:32:39 +0000824 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000825 __bucket_list_deallocator(__bucket_list_deallocator&& __x)
Howard Hinnant37141072011-06-04 18:54:24 +0000826 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnantce48a112011-06-30 21:18:19 +0000827 : __data_(_VSTD::move(__x.__data_))
Howard Hinnant3e519522010-05-11 19:42:16 +0000828 {
829 __x.size() = 0;
830 }
831
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000832#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +0000833
Howard Hinnant37141072011-06-04 18:54:24 +0000834 _LIBCPP_INLINE_VISIBILITY
835 size_type& size() _NOEXCEPT {return __data_.first();}
836 _LIBCPP_INLINE_VISIBILITY
837 size_type size() const _NOEXCEPT {return __data_.first();}
Howard Hinnant3e519522010-05-11 19:42:16 +0000838
Howard Hinnant43d99232010-09-21 17:32:39 +0000839 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +0000840 allocator_type& __alloc() _NOEXCEPT {return __data_.second();}
841 _LIBCPP_INLINE_VISIBILITY
842 const allocator_type& __alloc() const _NOEXCEPT {return __data_.second();}
843
844 _LIBCPP_INLINE_VISIBILITY
845 void operator()(pointer __p) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000846 {
847 __alloc_traits::deallocate(__alloc(), __p, size());
848 }
849};
850
Howard Hinnantce534202011-06-14 19:58:17 +0000851template <class _Alloc> class __hash_map_node_destructor;
Howard Hinnant3e519522010-05-11 19:42:16 +0000852
853template <class _Alloc>
854class __hash_node_destructor
855{
856 typedef _Alloc allocator_type;
857 typedef allocator_traits<allocator_type> __alloc_traits;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000858
Howard Hinnant3e519522010-05-11 19:42:16 +0000859public:
860 typedef typename __alloc_traits::pointer pointer;
861private:
Eric Fiselierfcd02212016-02-11 11:59:44 +0000862 typedef __hash_node_types<pointer> _NodeTypes;
Howard Hinnant3e519522010-05-11 19:42:16 +0000863
864 allocator_type& __na_;
865
866 __hash_node_destructor& operator=(const __hash_node_destructor&);
867
868public:
869 bool __value_constructed;
870
Howard Hinnant43d99232010-09-21 17:32:39 +0000871 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf622b582011-07-31 17:04:30 +0000872 explicit __hash_node_destructor(allocator_type& __na,
873 bool __constructed = false) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000874 : __na_(__na),
Howard Hinnantf622b582011-07-31 17:04:30 +0000875 __value_constructed(__constructed)
Howard Hinnant3e519522010-05-11 19:42:16 +0000876 {}
877
Howard Hinnant43d99232010-09-21 17:32:39 +0000878 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +0000879 void operator()(pointer __p) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000880 {
881 if (__value_constructed)
Eric Fiselierfcd02212016-02-11 11:59:44 +0000882 __alloc_traits::destroy(__na_, _NodeTypes::__get_ptr(__p->__value_));
Howard Hinnant3e519522010-05-11 19:42:16 +0000883 if (__p)
884 __alloc_traits::deallocate(__na_, __p, 1);
885 }
886
887 template <class> friend class __hash_map_node_destructor;
888};
889
890template <class _Tp, class _Hash, class _Equal, class _Alloc>
891class __hash_table
892{
893public:
894 typedef _Tp value_type;
895 typedef _Hash hasher;
896 typedef _Equal key_equal;
897 typedef _Alloc allocator_type;
898
899private:
900 typedef allocator_traits<allocator_type> __alloc_traits;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000901 typedef typename
902 __make_hash_node_types<value_type, typename __alloc_traits::void_pointer>::type
903 _NodeTypes;
Howard Hinnant3e519522010-05-11 19:42:16 +0000904public:
Eric Fiselierfcd02212016-02-11 11:59:44 +0000905
906 typedef typename _NodeTypes::__node_value_type __node_value_type;
907 typedef typename _NodeTypes::__container_value_type __container_value_type;
Duncan P. N. Exon Smithfde79b42016-03-17 20:45:20 +0000908 typedef typename _NodeTypes::key_type key_type;
Howard Hinnant3e519522010-05-11 19:42:16 +0000909 typedef value_type& reference;
910 typedef const value_type& const_reference;
911 typedef typename __alloc_traits::pointer pointer;
912 typedef typename __alloc_traits::const_pointer const_pointer;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000913#ifndef _LIBCPP_ABI_FIX_UNORDERED_CONTAINER_SIZE_TYPE
Howard Hinnant3e519522010-05-11 19:42:16 +0000914 typedef typename __alloc_traits::size_type size_type;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000915#else
916 typedef typename _NodeTypes::size_type size_type;
917#endif
918 typedef typename _NodeTypes::difference_type difference_type;
Howard Hinnant3e519522010-05-11 19:42:16 +0000919public:
920 // Create __node
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000921
922 typedef typename _NodeTypes::__node_type __node;
Marshall Clow1f508012015-04-07 05:21:38 +0000923 typedef typename __rebind_alloc_helper<__alloc_traits, __node>::type __node_allocator;
Howard Hinnant3e519522010-05-11 19:42:16 +0000924 typedef allocator_traits<__node_allocator> __node_traits;
Eric Fiselier8e397682016-02-11 15:22:37 +0000925 typedef typename _NodeTypes::__void_pointer __void_pointer;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000926 typedef typename _NodeTypes::__node_pointer __node_pointer;
927 typedef typename _NodeTypes::__node_pointer __node_const_pointer;
928 typedef typename _NodeTypes::__node_base_type __first_node;
929 typedef typename _NodeTypes::__node_base_pointer __node_base_pointer;
930
931private:
932 // check for sane allocator pointer rebinding semantics. Rebinding the
933 // allocator for a new pointer type should be exactly the same as rebinding
934 // the pointer using 'pointer_traits'.
935 static_assert((is_same<__node_pointer, typename __node_traits::pointer>::value),
936 "Allocator does not rebind pointers in a sane manner.");
937 typedef typename __rebind_alloc_helper<__node_traits, __first_node>::type
938 __node_base_allocator;
939 typedef allocator_traits<__node_base_allocator> __node_base_traits;
940 static_assert((is_same<__node_base_pointer, typename __node_base_traits::pointer>::value),
941 "Allocator does not rebind pointers in a sane manner.");
Howard Hinnant3e519522010-05-11 19:42:16 +0000942
943private:
944
Marshall Clow1f508012015-04-07 05:21:38 +0000945 typedef typename __rebind_alloc_helper<__node_traits, __node_pointer>::type __pointer_allocator;
Howard Hinnant3e519522010-05-11 19:42:16 +0000946 typedef __bucket_list_deallocator<__pointer_allocator> __bucket_list_deleter;
947 typedef unique_ptr<__node_pointer[], __bucket_list_deleter> __bucket_list;
948 typedef allocator_traits<__pointer_allocator> __pointer_alloc_traits;
949 typedef typename __bucket_list_deleter::pointer __node_pointer_pointer;
950
951 // --- Member data begin ---
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000952 __bucket_list __bucket_list_;
953 __compressed_pair<__first_node, __node_allocator> __p1_;
954 __compressed_pair<size_type, hasher> __p2_;
955 __compressed_pair<float, key_equal> __p3_;
Howard Hinnant3e519522010-05-11 19:42:16 +0000956 // --- Member data end ---
957
Howard Hinnant37141072011-06-04 18:54:24 +0000958 _LIBCPP_INLINE_VISIBILITY
959 size_type& size() _NOEXCEPT {return __p2_.first();}
Howard Hinnant3e519522010-05-11 19:42:16 +0000960public:
Howard Hinnant37141072011-06-04 18:54:24 +0000961 _LIBCPP_INLINE_VISIBILITY
962 size_type size() const _NOEXCEPT {return __p2_.first();}
Howard Hinnant3e519522010-05-11 19:42:16 +0000963
Howard Hinnant37141072011-06-04 18:54:24 +0000964 _LIBCPP_INLINE_VISIBILITY
965 hasher& hash_function() _NOEXCEPT {return __p2_.second();}
966 _LIBCPP_INLINE_VISIBILITY
967 const hasher& hash_function() const _NOEXCEPT {return __p2_.second();}
Howard Hinnant3e519522010-05-11 19:42:16 +0000968
Howard Hinnant37141072011-06-04 18:54:24 +0000969 _LIBCPP_INLINE_VISIBILITY
970 float& max_load_factor() _NOEXCEPT {return __p3_.first();}
971 _LIBCPP_INLINE_VISIBILITY
972 float max_load_factor() const _NOEXCEPT {return __p3_.first();}
Howard Hinnant3e519522010-05-11 19:42:16 +0000973
Howard Hinnant37141072011-06-04 18:54:24 +0000974 _LIBCPP_INLINE_VISIBILITY
975 key_equal& key_eq() _NOEXCEPT {return __p3_.second();}
976 _LIBCPP_INLINE_VISIBILITY
977 const key_equal& key_eq() const _NOEXCEPT {return __p3_.second();}
Howard Hinnant3e519522010-05-11 19:42:16 +0000978
Howard Hinnant37141072011-06-04 18:54:24 +0000979 _LIBCPP_INLINE_VISIBILITY
980 __node_allocator& __node_alloc() _NOEXCEPT {return __p1_.second();}
981 _LIBCPP_INLINE_VISIBILITY
982 const __node_allocator& __node_alloc() const _NOEXCEPT
983 {return __p1_.second();}
Howard Hinnant3e519522010-05-11 19:42:16 +0000984
985public:
986 typedef __hash_iterator<__node_pointer> iterator;
Howard Hinnant307f8142013-06-22 15:21:29 +0000987 typedef __hash_const_iterator<__node_pointer> const_iterator;
Howard Hinnant3e519522010-05-11 19:42:16 +0000988 typedef __hash_local_iterator<__node_pointer> local_iterator;
Howard Hinnant307f8142013-06-22 15:21:29 +0000989 typedef __hash_const_local_iterator<__node_pointer> const_local_iterator;
Howard Hinnant3e519522010-05-11 19:42:16 +0000990
Evgeniy Stepanov906c8722015-11-07 01:22:13 +0000991 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +0000992 __hash_table()
993 _NOEXCEPT_(
994 is_nothrow_default_constructible<__bucket_list>::value &&
995 is_nothrow_default_constructible<__first_node>::value &&
996 is_nothrow_default_constructible<__node_allocator>::value &&
997 is_nothrow_default_constructible<hasher>::value &&
998 is_nothrow_default_constructible<key_equal>::value);
Evgeniy Stepanov906c8722015-11-07 01:22:13 +0000999 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001000 __hash_table(const hasher& __hf, const key_equal& __eql);
1001 __hash_table(const hasher& __hf, const key_equal& __eql,
1002 const allocator_type& __a);
1003 explicit __hash_table(const allocator_type& __a);
1004 __hash_table(const __hash_table& __u);
1005 __hash_table(const __hash_table& __u, const allocator_type& __a);
Eric Fiselierfcd02212016-02-11 11:59:44 +00001006#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant37141072011-06-04 18:54:24 +00001007 __hash_table(__hash_table&& __u)
1008 _NOEXCEPT_(
1009 is_nothrow_move_constructible<__bucket_list>::value &&
1010 is_nothrow_move_constructible<__first_node>::value &&
1011 is_nothrow_move_constructible<__node_allocator>::value &&
1012 is_nothrow_move_constructible<hasher>::value &&
1013 is_nothrow_move_constructible<key_equal>::value);
Howard Hinnant3e519522010-05-11 19:42:16 +00001014 __hash_table(__hash_table&& __u, const allocator_type& __a);
Eric Fiselierfcd02212016-02-11 11:59:44 +00001015#endif // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001016 ~__hash_table();
1017
1018 __hash_table& operator=(const __hash_table& __u);
Eric Fiselierfcd02212016-02-11 11:59:44 +00001019#ifndef _LIBCPP_CXX03_LANG
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001020 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001021 __hash_table& operator=(__hash_table&& __u)
1022 _NOEXCEPT_(
1023 __node_traits::propagate_on_container_move_assignment::value &&
1024 is_nothrow_move_assignable<__node_allocator>::value &&
1025 is_nothrow_move_assignable<hasher>::value &&
1026 is_nothrow_move_assignable<key_equal>::value);
Howard Hinnant3e519522010-05-11 19:42:16 +00001027#endif
1028 template <class _InputIterator>
1029 void __assign_unique(_InputIterator __first, _InputIterator __last);
1030 template <class _InputIterator>
1031 void __assign_multi(_InputIterator __first, _InputIterator __last);
1032
Howard Hinnant43d99232010-09-21 17:32:39 +00001033 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001034 size_type max_size() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001035 {
1036 return allocator_traits<__pointer_allocator>::max_size(
1037 __bucket_list_.get_deleter().__alloc());
1038 }
1039
1040 pair<iterator, bool> __node_insert_unique(__node_pointer __nd);
1041 iterator __node_insert_multi(__node_pointer __nd);
1042 iterator __node_insert_multi(const_iterator __p,
1043 __node_pointer __nd);
1044
Eric Fiselierfcd02212016-02-11 11:59:44 +00001045#ifndef _LIBCPP_CXX03_LANG
1046 template <class _Key, class ..._Args>
Duncan P. N. Exon Smithfde79b42016-03-17 20:45:20 +00001047 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierfcd02212016-02-11 11:59:44 +00001048 pair<iterator, bool> __emplace_unique_key_args(_Key const& __k, _Args&&... __args);
Howard Hinnant3e519522010-05-11 19:42:16 +00001049
Eric Fiselierfcd02212016-02-11 11:59:44 +00001050 template <class... _Args>
Duncan P. N. Exon Smithfde79b42016-03-17 20:45:20 +00001051 _LIBCPP_INLINE_VISIBILITY
1052 pair<iterator, bool> __emplace_unique_impl(_Args&&... __args);
1053
1054 template <class _Pp>
1055 _LIBCPP_INLINE_VISIBILITY
1056 pair<iterator, bool> __emplace_unique(_Pp&& __x) {
1057 return __emplace_unique_extract_key(_VSTD::forward<_Pp>(__x),
1058 __can_extract_key<_Pp, key_type>());
1059 }
Eric Fiselier50088682016-04-16 00:23:12 +00001060
1061 template <class _First, class _Second>
1062 _LIBCPP_INLINE_VISIBILITY
1063 typename enable_if<
1064 __can_extract_map_key<_First, key_type, __container_value_type>::value,
1065 pair<iterator, bool>
1066 >::type __emplace_unique(_First&& __f, _Second&& __s) {
1067 return __emplace_unique_key_args(__f, _VSTD::forward<_First>(__f),
1068 _VSTD::forward<_Second>(__s));
1069 }
1070
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(_Args&&... __args) {
1074 return __emplace_unique_impl(_VSTD::forward<_Args>(__args)...);
1075 }
1076
1077 template <class _Pp>
1078 _LIBCPP_INLINE_VISIBILITY
1079 pair<iterator, bool>
1080 __emplace_unique_extract_key(_Pp&& __x, __extract_key_fail_tag) {
1081 return __emplace_unique_impl(_VSTD::forward<_Pp>(__x));
1082 }
1083 template <class _Pp>
1084 _LIBCPP_INLINE_VISIBILITY
1085 pair<iterator, bool>
1086 __emplace_unique_extract_key(_Pp&& __x, __extract_key_self_tag) {
1087 return __emplace_unique_key_args(__x, _VSTD::forward<_Pp>(__x));
1088 }
1089 template <class _Pp>
1090 _LIBCPP_INLINE_VISIBILITY
1091 pair<iterator, bool>
1092 __emplace_unique_extract_key(_Pp&& __x, __extract_key_first_tag) {
1093 return __emplace_unique_key_args(__x.first, _VSTD::forward<_Pp>(__x));
1094 }
1095
1096 template <class... _Args>
1097 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierfcd02212016-02-11 11:59:44 +00001098 iterator __emplace_multi(_Args&&... __args);
1099 template <class... _Args>
Duncan P. N. Exon Smithfde79b42016-03-17 20:45:20 +00001100 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierfcd02212016-02-11 11:59:44 +00001101 iterator __emplace_hint_multi(const_iterator __p, _Args&&... __args);
1102
1103
Eric Fiselierde3f2b32015-06-13 07:18:32 +00001104 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierfcd02212016-02-11 11:59:44 +00001105 pair<iterator, bool>
1106 __insert_unique(__container_value_type&& __x) {
1107 return __emplace_unique_key_args(_NodeTypes::__get_key(__x), _VSTD::move(__x));
1108 }
1109
1110 template <class _Pp, class = typename enable_if<
1111 !__is_same_uncvref<_Pp, __container_value_type>::value
1112 >::type>
Eric Fiselierde3f2b32015-06-13 07:18:32 +00001113 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierfcd02212016-02-11 11:59:44 +00001114 pair<iterator, bool> __insert_unique(_Pp&& __x) {
1115 return __emplace_unique(_VSTD::forward<_Pp>(__x));
1116 }
1117
1118 template <class _Pp>
1119 _LIBCPP_INLINE_VISIBILITY
1120 iterator __insert_multi(_Pp&& __x) {
1121 return __emplace_multi(_VSTD::forward<_Pp>(__x));
1122 }
1123
1124 template <class _Pp>
1125 _LIBCPP_INLINE_VISIBILITY
1126 iterator __insert_multi(const_iterator __p, _Pp&& __x) {
1127 return __emplace_hint_multi(__p, _VSTD::forward<_Pp>(__x));
1128 }
1129
1130#else // !defined(_LIBCPP_CXX03_LANG)
1131 template <class _Key, class _Args>
1132 pair<iterator, bool> __emplace_unique_key_args(_Key const&, _Args& __args);
1133
1134 iterator __insert_multi(const __container_value_type& __x);
1135 iterator __insert_multi(const_iterator __p, const __container_value_type& __x);
Eric Fiselierde3f2b32015-06-13 07:18:32 +00001136#endif
1137
Eric Fiselierfcd02212016-02-11 11:59:44 +00001138 _LIBCPP_INLINE_VISIBILITY
1139 pair<iterator, bool> __insert_unique(const __container_value_type& __x) {
1140 return __emplace_unique_key_args(_NodeTypes::__get_key(__x), __x);
1141 }
Howard Hinnant3e519522010-05-11 19:42:16 +00001142
Howard Hinnant37141072011-06-04 18:54:24 +00001143 void clear() _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +00001144 void rehash(size_type __n);
Howard Hinnant43d99232010-09-21 17:32:39 +00001145 _LIBCPP_INLINE_VISIBILITY void reserve(size_type __n)
Howard Hinnant3e519522010-05-11 19:42:16 +00001146 {rehash(static_cast<size_type>(ceil(__n / max_load_factor())));}
Howard Hinnant43d99232010-09-21 17:32:39 +00001147
1148 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001149 size_type bucket_count() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001150 {
1151 return __bucket_list_.get_deleter().size();
1152 }
1153
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001154 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001155 iterator begin() _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001156 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001157 iterator end() _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001158 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001159 const_iterator begin() const _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001160 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001161 const_iterator end() const _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +00001162
1163 template <class _Key>
Howard Hinnant43d99232010-09-21 17:32:39 +00001164 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001165 size_type bucket(const _Key& __k) const
Howard Hinnante5c13de2013-07-29 19:05:47 +00001166 {
1167 _LIBCPP_ASSERT(bucket_count() > 0,
1168 "unordered container::bucket(key) called when bucket_count() == 0");
1169 return __constrain_hash(hash_function()(__k), bucket_count());
1170 }
Howard Hinnant3e519522010-05-11 19:42:16 +00001171
1172 template <class _Key>
1173 iterator find(const _Key& __x);
1174 template <class _Key>
1175 const_iterator find(const _Key& __x) const;
1176
Howard Hinnantc003db12011-11-29 18:15:50 +00001177 typedef __hash_node_destructor<__node_allocator> _Dp;
1178 typedef unique_ptr<__node, _Dp> __node_holder;
Howard Hinnant3e519522010-05-11 19:42:16 +00001179
1180 iterator erase(const_iterator __p);
1181 iterator erase(const_iterator __first, const_iterator __last);
1182 template <class _Key>
1183 size_type __erase_unique(const _Key& __k);
1184 template <class _Key>
1185 size_type __erase_multi(const _Key& __k);
Howard Hinnant37141072011-06-04 18:54:24 +00001186 __node_holder remove(const_iterator __p) _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +00001187
1188 template <class _Key>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001189 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001190 size_type __count_unique(const _Key& __k) const;
1191 template <class _Key>
1192 size_type __count_multi(const _Key& __k) const;
1193
1194 template <class _Key>
1195 pair<iterator, iterator>
1196 __equal_range_unique(const _Key& __k);
1197 template <class _Key>
1198 pair<const_iterator, const_iterator>
1199 __equal_range_unique(const _Key& __k) const;
1200
1201 template <class _Key>
1202 pair<iterator, iterator>
1203 __equal_range_multi(const _Key& __k);
1204 template <class _Key>
1205 pair<const_iterator, const_iterator>
1206 __equal_range_multi(const _Key& __k) const;
1207
Howard Hinnant37141072011-06-04 18:54:24 +00001208 void swap(__hash_table& __u)
Eric Fiselier87a82492015-07-18 20:40:46 +00001209#if _LIBCPP_STD_VER <= 11
Howard Hinnant37141072011-06-04 18:54:24 +00001210 _NOEXCEPT_(
Marshall Clowe3fbe142015-07-13 20:04:56 +00001211 __is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value
Marshall Clowe3fbe142015-07-13 20:04:56 +00001212 && (!allocator_traits<__pointer_allocator>::propagate_on_container_swap::value
1213 || __is_nothrow_swappable<__pointer_allocator>::value)
1214 && (!__node_traits::propagate_on_container_swap::value
1215 || __is_nothrow_swappable<__node_allocator>::value)
Marshall Clowe3fbe142015-07-13 20:04:56 +00001216 );
Eric Fiselier87a82492015-07-18 20:40:46 +00001217#else
1218 _NOEXCEPT_(__is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value);
1219#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001220
Howard Hinnant43d99232010-09-21 17:32:39 +00001221 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001222 size_type max_bucket_count() const _NOEXCEPT
Howard Hinnant307f8142013-06-22 15:21:29 +00001223 {return __pointer_alloc_traits::max_size(__bucket_list_.get_deleter().__alloc());}
Howard Hinnant3e519522010-05-11 19:42:16 +00001224 size_type bucket_size(size_type __n) const;
Howard Hinnant37141072011-06-04 18:54:24 +00001225 _LIBCPP_INLINE_VISIBILITY float load_factor() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001226 {
1227 size_type __bc = bucket_count();
1228 return __bc != 0 ? (float)size() / __bc : 0.f;
1229 }
Howard Hinnant37141072011-06-04 18:54:24 +00001230 _LIBCPP_INLINE_VISIBILITY void max_load_factor(float __mlf) _NOEXCEPT
Howard Hinnante5c13de2013-07-29 19:05:47 +00001231 {
1232 _LIBCPP_ASSERT(__mlf > 0,
1233 "unordered container::max_load_factor(lf) called with lf <= 0");
1234 max_load_factor() = _VSTD::max(__mlf, load_factor());
1235 }
Howard Hinnant3e519522010-05-11 19:42:16 +00001236
Howard Hinnantb24c8022013-07-23 22:01:58 +00001237 _LIBCPP_INLINE_VISIBILITY
1238 local_iterator
1239 begin(size_type __n)
1240 {
Howard Hinnante5c13de2013-07-29 19:05:47 +00001241 _LIBCPP_ASSERT(__n < bucket_count(),
1242 "unordered container::begin(n) called with n >= bucket_count()");
Howard Hinnantb24c8022013-07-23 22:01:58 +00001243#if _LIBCPP_DEBUG_LEVEL >= 2
1244 return local_iterator(__bucket_list_[__n], __n, bucket_count(), this);
1245#else
1246 return local_iterator(__bucket_list_[__n], __n, bucket_count());
1247#endif
1248 }
1249
1250 _LIBCPP_INLINE_VISIBILITY
1251 local_iterator
1252 end(size_type __n)
1253 {
Howard Hinnante5c13de2013-07-29 19:05:47 +00001254 _LIBCPP_ASSERT(__n < bucket_count(),
1255 "unordered container::end(n) called with n >= bucket_count()");
Howard Hinnantb24c8022013-07-23 22:01:58 +00001256#if _LIBCPP_DEBUG_LEVEL >= 2
1257 return local_iterator(nullptr, __n, bucket_count(), this);
1258#else
1259 return local_iterator(nullptr, __n, bucket_count());
1260#endif
1261 }
1262
1263 _LIBCPP_INLINE_VISIBILITY
1264 const_local_iterator
1265 cbegin(size_type __n) const
1266 {
Howard Hinnante5c13de2013-07-29 19:05:47 +00001267 _LIBCPP_ASSERT(__n < bucket_count(),
1268 "unordered container::cbegin(n) called with n >= bucket_count()");
Howard Hinnantb24c8022013-07-23 22:01:58 +00001269#if _LIBCPP_DEBUG_LEVEL >= 2
1270 return const_local_iterator(__bucket_list_[__n], __n, bucket_count(), this);
1271#else
1272 return const_local_iterator(__bucket_list_[__n], __n, bucket_count());
1273#endif
1274 }
1275
1276 _LIBCPP_INLINE_VISIBILITY
1277 const_local_iterator
1278 cend(size_type __n) const
1279 {
Howard Hinnante5c13de2013-07-29 19:05:47 +00001280 _LIBCPP_ASSERT(__n < bucket_count(),
1281 "unordered container::cend(n) called with n >= bucket_count()");
Howard Hinnantb24c8022013-07-23 22:01:58 +00001282#if _LIBCPP_DEBUG_LEVEL >= 2
1283 return const_local_iterator(nullptr, __n, bucket_count(), this);
1284#else
1285 return const_local_iterator(nullptr, __n, bucket_count());
1286#endif
1287 }
1288
1289#if _LIBCPP_DEBUG_LEVEL >= 2
1290
1291 bool __dereferenceable(const const_iterator* __i) const;
1292 bool __decrementable(const const_iterator* __i) const;
1293 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1294 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1295
1296#endif // _LIBCPP_DEBUG_LEVEL >= 2
1297
Howard Hinnant3e519522010-05-11 19:42:16 +00001298private:
1299 void __rehash(size_type __n);
1300
Eric Fiselierfcd02212016-02-11 11:59:44 +00001301#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001302 template <class ..._Args>
Eric Fiselierfcd02212016-02-11 11:59:44 +00001303 __node_holder __construct_node(_Args&& ...__args);
1304
1305 template <class _First, class ..._Rest>
1306 __node_holder __construct_node_hash(size_t __hash, _First&& __f, _Rest&&... __rest);
1307#else // _LIBCPP_CXX03_LANG
1308 __node_holder __construct_node(const __container_value_type& __v);
1309 __node_holder __construct_node_hash(size_t __hash, const __container_value_type& __v);
Howard Hinnant3e519522010-05-11 19:42:16 +00001310#endif
Eric Fiselierfcd02212016-02-11 11:59:44 +00001311
Howard Hinnant3e519522010-05-11 19:42:16 +00001312
Howard Hinnant43d99232010-09-21 17:32:39 +00001313 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001314 void __copy_assign_alloc(const __hash_table& __u)
1315 {__copy_assign_alloc(__u, integral_constant<bool,
1316 __node_traits::propagate_on_container_copy_assignment::value>());}
1317 void __copy_assign_alloc(const __hash_table& __u, true_type);
Howard Hinnant43d99232010-09-21 17:32:39 +00001318 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2063662011-12-01 20:21:04 +00001319 void __copy_assign_alloc(const __hash_table&, false_type) {}
Howard Hinnant3e519522010-05-11 19:42:16 +00001320
Eric Fiselierfcd02212016-02-11 11:59:44 +00001321#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001322 void __move_assign(__hash_table& __u, false_type);
Howard Hinnant37141072011-06-04 18:54:24 +00001323 void __move_assign(__hash_table& __u, true_type)
1324 _NOEXCEPT_(
1325 is_nothrow_move_assignable<__node_allocator>::value &&
1326 is_nothrow_move_assignable<hasher>::value &&
1327 is_nothrow_move_assignable<key_equal>::value);
1328 _LIBCPP_INLINE_VISIBILITY
1329 void __move_assign_alloc(__hash_table& __u)
1330 _NOEXCEPT_(
1331 !__node_traits::propagate_on_container_move_assignment::value ||
1332 (is_nothrow_move_assignable<__pointer_allocator>::value &&
1333 is_nothrow_move_assignable<__node_allocator>::value))
Howard Hinnant3e519522010-05-11 19:42:16 +00001334 {__move_assign_alloc(__u, integral_constant<bool,
1335 __node_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnant43d99232010-09-21 17:32:39 +00001336 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001337 void __move_assign_alloc(__hash_table& __u, true_type)
Howard Hinnant37141072011-06-04 18:54:24 +00001338 _NOEXCEPT_(
1339 is_nothrow_move_assignable<__pointer_allocator>::value &&
1340 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +00001341 {
1342 __bucket_list_.get_deleter().__alloc() =
Howard Hinnantce48a112011-06-30 21:18:19 +00001343 _VSTD::move(__u.__bucket_list_.get_deleter().__alloc());
1344 __node_alloc() = _VSTD::move(__u.__node_alloc());
Howard Hinnant3e519522010-05-11 19:42:16 +00001345 }
Howard Hinnant43d99232010-09-21 17:32:39 +00001346 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001347 void __move_assign_alloc(__hash_table&, false_type) _NOEXCEPT {}
Eric Fiselierfcd02212016-02-11 11:59:44 +00001348#endif // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001349
Howard Hinnant37141072011-06-04 18:54:24 +00001350 void __deallocate(__node_pointer __np) _NOEXCEPT;
1351 __node_pointer __detach() _NOEXCEPT;
Howard Hinnant307f8142013-06-22 15:21:29 +00001352
Howard Hinnantf0544c22013-08-12 18:38:34 +00001353 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_map;
1354 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_multimap;
Howard Hinnant3e519522010-05-11 19:42:16 +00001355};
1356
1357template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001358inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001359__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table()
Howard Hinnant37141072011-06-04 18:54:24 +00001360 _NOEXCEPT_(
1361 is_nothrow_default_constructible<__bucket_list>::value &&
1362 is_nothrow_default_constructible<__first_node>::value &&
Eric Fiseliere2e332a2015-12-16 00:53:04 +00001363 is_nothrow_default_constructible<__node_allocator>::value &&
Howard Hinnant37141072011-06-04 18:54:24 +00001364 is_nothrow_default_constructible<hasher>::value &&
1365 is_nothrow_default_constructible<key_equal>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +00001366 : __p2_(0),
1367 __p3_(1.0f)
1368{
1369}
1370
1371template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001372inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001373__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const hasher& __hf,
1374 const key_equal& __eql)
1375 : __bucket_list_(nullptr, __bucket_list_deleter()),
1376 __p1_(),
1377 __p2_(0, __hf),
1378 __p3_(1.0f, __eql)
1379{
1380}
1381
1382template <class _Tp, class _Hash, class _Equal, class _Alloc>
1383__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const hasher& __hf,
1384 const key_equal& __eql,
1385 const allocator_type& __a)
1386 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1387 __p1_(__node_allocator(__a)),
1388 __p2_(0, __hf),
1389 __p3_(1.0f, __eql)
1390{
1391}
1392
1393template <class _Tp, class _Hash, class _Equal, class _Alloc>
1394__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const allocator_type& __a)
1395 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1396 __p1_(__node_allocator(__a)),
1397 __p2_(0),
1398 __p3_(1.0f)
1399{
1400}
1401
1402template <class _Tp, class _Hash, class _Equal, class _Alloc>
1403__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const __hash_table& __u)
1404 : __bucket_list_(nullptr,
1405 __bucket_list_deleter(allocator_traits<__pointer_allocator>::
1406 select_on_container_copy_construction(
1407 __u.__bucket_list_.get_deleter().__alloc()), 0)),
1408 __p1_(allocator_traits<__node_allocator>::
1409 select_on_container_copy_construction(__u.__node_alloc())),
1410 __p2_(0, __u.hash_function()),
1411 __p3_(__u.__p3_)
1412{
1413}
1414
1415template <class _Tp, class _Hash, class _Equal, class _Alloc>
1416__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const __hash_table& __u,
1417 const allocator_type& __a)
1418 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1419 __p1_(__node_allocator(__a)),
1420 __p2_(0, __u.hash_function()),
1421 __p3_(__u.__p3_)
1422{
1423}
1424
Eric Fiselierfcd02212016-02-11 11:59:44 +00001425#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001426
1427template <class _Tp, class _Hash, class _Equal, class _Alloc>
1428__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(__hash_table&& __u)
Howard Hinnant37141072011-06-04 18:54:24 +00001429 _NOEXCEPT_(
1430 is_nothrow_move_constructible<__bucket_list>::value &&
1431 is_nothrow_move_constructible<__first_node>::value &&
Eric Fiseliere2e332a2015-12-16 00:53:04 +00001432 is_nothrow_move_constructible<__node_allocator>::value &&
Howard Hinnant37141072011-06-04 18:54:24 +00001433 is_nothrow_move_constructible<hasher>::value &&
1434 is_nothrow_move_constructible<key_equal>::value)
Howard Hinnantce48a112011-06-30 21:18:19 +00001435 : __bucket_list_(_VSTD::move(__u.__bucket_list_)),
1436 __p1_(_VSTD::move(__u.__p1_)),
1437 __p2_(_VSTD::move(__u.__p2_)),
1438 __p3_(_VSTD::move(__u.__p3_))
Howard Hinnant3e519522010-05-11 19:42:16 +00001439{
1440 if (size() > 0)
1441 {
Howard Hinnant4cb38a82012-07-06 17:31:14 +00001442 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash_, bucket_count())] =
Howard Hinnant307f8142013-06-22 15:21:29 +00001443 static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
Howard Hinnant3e519522010-05-11 19:42:16 +00001444 __u.__p1_.first().__next_ = nullptr;
1445 __u.size() = 0;
1446 }
1447}
1448
1449template <class _Tp, class _Hash, class _Equal, class _Alloc>
1450__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(__hash_table&& __u,
1451 const allocator_type& __a)
1452 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1453 __p1_(__node_allocator(__a)),
Howard Hinnantce48a112011-06-30 21:18:19 +00001454 __p2_(0, _VSTD::move(__u.hash_function())),
1455 __p3_(_VSTD::move(__u.__p3_))
Howard Hinnant3e519522010-05-11 19:42:16 +00001456{
1457 if (__a == allocator_type(__u.__node_alloc()))
1458 {
1459 __bucket_list_.reset(__u.__bucket_list_.release());
1460 __bucket_list_.get_deleter().size() = __u.__bucket_list_.get_deleter().size();
1461 __u.__bucket_list_.get_deleter().size() = 0;
1462 if (__u.size() > 0)
1463 {
1464 __p1_.first().__next_ = __u.__p1_.first().__next_;
1465 __u.__p1_.first().__next_ = nullptr;
Howard Hinnant4cb38a82012-07-06 17:31:14 +00001466 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash_, bucket_count())] =
Howard Hinnant307f8142013-06-22 15:21:29 +00001467 static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
Howard Hinnant3e519522010-05-11 19:42:16 +00001468 size() = __u.size();
1469 __u.size() = 0;
1470 }
1471 }
1472}
1473
Eric Fiselierfcd02212016-02-11 11:59:44 +00001474#endif // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001475
1476template <class _Tp, class _Hash, class _Equal, class _Alloc>
1477__hash_table<_Tp, _Hash, _Equal, _Alloc>::~__hash_table()
1478{
Marshall Clow3b8669e2016-06-30 22:05:45 +00001479 static_assert((is_copy_constructible<key_equal>::value),
1480 "Predicate must be copy-constructible.");
1481 static_assert((is_copy_constructible<hasher>::value),
1482 "Hasher must be copy-constructible.");
Howard Hinnant3e519522010-05-11 19:42:16 +00001483 __deallocate(__p1_.first().__next_);
Howard Hinnantb24c8022013-07-23 22:01:58 +00001484#if _LIBCPP_DEBUG_LEVEL >= 2
1485 __get_db()->__erase_c(this);
1486#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001487}
1488
1489template <class _Tp, class _Hash, class _Equal, class _Alloc>
1490void
1491__hash_table<_Tp, _Hash, _Equal, _Alloc>::__copy_assign_alloc(
1492 const __hash_table& __u, true_type)
1493{
1494 if (__node_alloc() != __u.__node_alloc())
1495 {
1496 clear();
1497 __bucket_list_.reset();
1498 __bucket_list_.get_deleter().size() = 0;
1499 }
1500 __bucket_list_.get_deleter().__alloc() = __u.__bucket_list_.get_deleter().__alloc();
1501 __node_alloc() = __u.__node_alloc();
1502}
1503
1504template <class _Tp, class _Hash, class _Equal, class _Alloc>
1505__hash_table<_Tp, _Hash, _Equal, _Alloc>&
1506__hash_table<_Tp, _Hash, _Equal, _Alloc>::operator=(const __hash_table& __u)
1507{
1508 if (this != &__u)
1509 {
1510 __copy_assign_alloc(__u);
1511 hash_function() = __u.hash_function();
1512 key_eq() = __u.key_eq();
1513 max_load_factor() = __u.max_load_factor();
1514 __assign_multi(__u.begin(), __u.end());
1515 }
1516 return *this;
1517}
1518
1519template <class _Tp, class _Hash, class _Equal, class _Alloc>
1520void
1521__hash_table<_Tp, _Hash, _Equal, _Alloc>::__deallocate(__node_pointer __np)
Howard Hinnant37141072011-06-04 18:54:24 +00001522 _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001523{
1524 __node_allocator& __na = __node_alloc();
1525 while (__np != nullptr)
1526 {
1527 __node_pointer __next = __np->__next_;
Howard Hinnantb24c8022013-07-23 22:01:58 +00001528#if _LIBCPP_DEBUG_LEVEL >= 2
1529 __c_node* __c = __get_db()->__find_c_and_lock(this);
1530 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1531 {
1532 --__p;
1533 iterator* __i = static_cast<iterator*>((*__p)->__i_);
1534 if (__i->__node_ == __np)
1535 {
1536 (*__p)->__c_ = nullptr;
1537 if (--__c->end_ != __p)
1538 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1539 }
1540 }
1541 __get_db()->unlock();
1542#endif
Eric Fiselierfcd02212016-02-11 11:59:44 +00001543 __node_traits::destroy(__na, _NodeTypes::__get_ptr(__np->__value_));
Howard Hinnant3e519522010-05-11 19:42:16 +00001544 __node_traits::deallocate(__na, __np, 1);
1545 __np = __next;
1546 }
1547}
1548
1549template <class _Tp, class _Hash, class _Equal, class _Alloc>
1550typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_pointer
Howard Hinnant37141072011-06-04 18:54:24 +00001551__hash_table<_Tp, _Hash, _Equal, _Alloc>::__detach() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001552{
1553 size_type __bc = bucket_count();
1554 for (size_type __i = 0; __i < __bc; ++__i)
1555 __bucket_list_[__i] = nullptr;
1556 size() = 0;
1557 __node_pointer __cache = __p1_.first().__next_;
1558 __p1_.first().__next_ = nullptr;
1559 return __cache;
1560}
1561
Eric Fiselierfcd02212016-02-11 11:59:44 +00001562#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001563
1564template <class _Tp, class _Hash, class _Equal, class _Alloc>
1565void
1566__hash_table<_Tp, _Hash, _Equal, _Alloc>::__move_assign(
1567 __hash_table& __u, true_type)
Howard Hinnant37141072011-06-04 18:54:24 +00001568 _NOEXCEPT_(
1569 is_nothrow_move_assignable<__node_allocator>::value &&
1570 is_nothrow_move_assignable<hasher>::value &&
1571 is_nothrow_move_assignable<key_equal>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +00001572{
1573 clear();
1574 __bucket_list_.reset(__u.__bucket_list_.release());
1575 __bucket_list_.get_deleter().size() = __u.__bucket_list_.get_deleter().size();
1576 __u.__bucket_list_.get_deleter().size() = 0;
1577 __move_assign_alloc(__u);
1578 size() = __u.size();
Howard Hinnantce48a112011-06-30 21:18:19 +00001579 hash_function() = _VSTD::move(__u.hash_function());
Howard Hinnant3e519522010-05-11 19:42:16 +00001580 max_load_factor() = __u.max_load_factor();
Howard Hinnantce48a112011-06-30 21:18:19 +00001581 key_eq() = _VSTD::move(__u.key_eq());
Howard Hinnant3e519522010-05-11 19:42:16 +00001582 __p1_.first().__next_ = __u.__p1_.first().__next_;
1583 if (size() > 0)
1584 {
Howard Hinnant4cb38a82012-07-06 17:31:14 +00001585 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash_, bucket_count())] =
Howard Hinnant307f8142013-06-22 15:21:29 +00001586 static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
Howard Hinnant3e519522010-05-11 19:42:16 +00001587 __u.__p1_.first().__next_ = nullptr;
1588 __u.size() = 0;
1589 }
Howard Hinnantb24c8022013-07-23 22:01:58 +00001590#if _LIBCPP_DEBUG_LEVEL >= 2
1591 __get_db()->swap(this, &__u);
1592#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001593}
1594
1595template <class _Tp, class _Hash, class _Equal, class _Alloc>
1596void
1597__hash_table<_Tp, _Hash, _Equal, _Alloc>::__move_assign(
1598 __hash_table& __u, false_type)
1599{
1600 if (__node_alloc() == __u.__node_alloc())
1601 __move_assign(__u, true_type());
1602 else
1603 {
Howard Hinnantce48a112011-06-30 21:18:19 +00001604 hash_function() = _VSTD::move(__u.hash_function());
1605 key_eq() = _VSTD::move(__u.key_eq());
Howard Hinnant3e519522010-05-11 19:42:16 +00001606 max_load_factor() = __u.max_load_factor();
1607 if (bucket_count() != 0)
1608 {
1609 __node_pointer __cache = __detach();
1610#ifndef _LIBCPP_NO_EXCEPTIONS
1611 try
1612 {
Howard Hinnantb3371f62010-08-22 00:02:43 +00001613#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001614 const_iterator __i = __u.begin();
1615 while (__cache != nullptr && __u.size() != 0)
1616 {
Howard Hinnantce48a112011-06-30 21:18:19 +00001617 __cache->__value_ = _VSTD::move(__u.remove(__i++)->__value_);
Howard Hinnant3e519522010-05-11 19:42:16 +00001618 __node_pointer __next = __cache->__next_;
1619 __node_insert_multi(__cache);
1620 __cache = __next;
1621 }
1622#ifndef _LIBCPP_NO_EXCEPTIONS
1623 }
1624 catch (...)
1625 {
1626 __deallocate(__cache);
1627 throw;
1628 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00001629#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001630 __deallocate(__cache);
1631 }
1632 const_iterator __i = __u.begin();
1633 while (__u.size() != 0)
1634 {
Eric Fiselierfcd02212016-02-11 11:59:44 +00001635 __node_holder __h = __construct_node(_NodeTypes::__move(__u.remove(__i++)->__value_));
Howard Hinnant3e519522010-05-11 19:42:16 +00001636 __node_insert_multi(__h.get());
1637 __h.release();
1638 }
1639 }
1640}
1641
1642template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001643inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001644__hash_table<_Tp, _Hash, _Equal, _Alloc>&
1645__hash_table<_Tp, _Hash, _Equal, _Alloc>::operator=(__hash_table&& __u)
Howard Hinnant37141072011-06-04 18:54:24 +00001646 _NOEXCEPT_(
1647 __node_traits::propagate_on_container_move_assignment::value &&
1648 is_nothrow_move_assignable<__node_allocator>::value &&
1649 is_nothrow_move_assignable<hasher>::value &&
1650 is_nothrow_move_assignable<key_equal>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +00001651{
1652 __move_assign(__u, integral_constant<bool,
1653 __node_traits::propagate_on_container_move_assignment::value>());
1654 return *this;
1655}
1656
Eric Fiselierfcd02212016-02-11 11:59:44 +00001657#endif // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001658
1659template <class _Tp, class _Hash, class _Equal, class _Alloc>
1660template <class _InputIterator>
1661void
1662__hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_unique(_InputIterator __first,
1663 _InputIterator __last)
1664{
Eric Fiselierfcd02212016-02-11 11:59:44 +00001665 typedef iterator_traits<_InputIterator> _ITraits;
1666 typedef typename _ITraits::value_type _ItValueType;
1667 static_assert((is_same<_ItValueType, __container_value_type>::value),
1668 "__assign_unique may only be called with the containers value type");
1669
Howard Hinnant3e519522010-05-11 19:42:16 +00001670 if (bucket_count() != 0)
1671 {
1672 __node_pointer __cache = __detach();
1673#ifndef _LIBCPP_NO_EXCEPTIONS
1674 try
1675 {
Howard Hinnantb3371f62010-08-22 00:02:43 +00001676#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001677 for (; __cache != nullptr && __first != __last; ++__first)
1678 {
1679 __cache->__value_ = *__first;
1680 __node_pointer __next = __cache->__next_;
1681 __node_insert_unique(__cache);
1682 __cache = __next;
1683 }
1684#ifndef _LIBCPP_NO_EXCEPTIONS
1685 }
1686 catch (...)
1687 {
1688 __deallocate(__cache);
1689 throw;
1690 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00001691#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001692 __deallocate(__cache);
1693 }
1694 for (; __first != __last; ++__first)
1695 __insert_unique(*__first);
1696}
1697
1698template <class _Tp, class _Hash, class _Equal, class _Alloc>
1699template <class _InputIterator>
1700void
1701__hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_multi(_InputIterator __first,
1702 _InputIterator __last)
1703{
Eric Fiselierfcd02212016-02-11 11:59:44 +00001704 typedef iterator_traits<_InputIterator> _ITraits;
1705 typedef typename _ITraits::value_type _ItValueType;
1706 static_assert((is_same<_ItValueType, __container_value_type>::value ||
1707 is_same<_ItValueType, __node_value_type>::value),
1708 "__assign_multi may only be called with the containers value type"
1709 " or the nodes value type");
Howard Hinnant3e519522010-05-11 19:42:16 +00001710 if (bucket_count() != 0)
1711 {
1712 __node_pointer __cache = __detach();
1713#ifndef _LIBCPP_NO_EXCEPTIONS
1714 try
1715 {
Howard Hinnantb3371f62010-08-22 00:02:43 +00001716#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001717 for (; __cache != nullptr && __first != __last; ++__first)
1718 {
1719 __cache->__value_ = *__first;
1720 __node_pointer __next = __cache->__next_;
1721 __node_insert_multi(__cache);
1722 __cache = __next;
1723 }
1724#ifndef _LIBCPP_NO_EXCEPTIONS
1725 }
1726 catch (...)
1727 {
1728 __deallocate(__cache);
1729 throw;
1730 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00001731#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001732 __deallocate(__cache);
1733 }
1734 for (; __first != __last; ++__first)
Eric Fiselierfcd02212016-02-11 11:59:44 +00001735 __insert_multi(_NodeTypes::__get_value(*__first));
Howard Hinnant3e519522010-05-11 19:42:16 +00001736}
1737
1738template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001739inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001740typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
Howard Hinnant37141072011-06-04 18:54:24 +00001741__hash_table<_Tp, _Hash, _Equal, _Alloc>::begin() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001742{
Howard Hinnantb24c8022013-07-23 22:01:58 +00001743#if _LIBCPP_DEBUG_LEVEL >= 2
1744 return iterator(__p1_.first().__next_, this);
1745#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001746 return iterator(__p1_.first().__next_);
Howard Hinnantb24c8022013-07-23 22:01:58 +00001747#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001748}
1749
1750template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001751inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001752typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
Howard Hinnant37141072011-06-04 18:54:24 +00001753__hash_table<_Tp, _Hash, _Equal, _Alloc>::end() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001754{
Howard Hinnantb24c8022013-07-23 22:01:58 +00001755#if _LIBCPP_DEBUG_LEVEL >= 2
1756 return iterator(nullptr, this);
1757#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001758 return iterator(nullptr);
Howard Hinnantb24c8022013-07-23 22:01:58 +00001759#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001760}
1761
1762template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001763inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001764typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
Howard Hinnant37141072011-06-04 18:54:24 +00001765__hash_table<_Tp, _Hash, _Equal, _Alloc>::begin() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001766{
Howard Hinnantb24c8022013-07-23 22:01:58 +00001767#if _LIBCPP_DEBUG_LEVEL >= 2
1768 return const_iterator(__p1_.first().__next_, this);
1769#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001770 return const_iterator(__p1_.first().__next_);
Howard Hinnantb24c8022013-07-23 22:01:58 +00001771#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001772}
1773
1774template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001775inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001776typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
Howard Hinnant37141072011-06-04 18:54:24 +00001777__hash_table<_Tp, _Hash, _Equal, _Alloc>::end() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001778{
Howard Hinnantb24c8022013-07-23 22:01:58 +00001779#if _LIBCPP_DEBUG_LEVEL >= 2
1780 return const_iterator(nullptr, this);
1781#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001782 return const_iterator(nullptr);
Howard Hinnantb24c8022013-07-23 22:01:58 +00001783#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001784}
1785
1786template <class _Tp, class _Hash, class _Equal, class _Alloc>
1787void
Howard Hinnant37141072011-06-04 18:54:24 +00001788__hash_table<_Tp, _Hash, _Equal, _Alloc>::clear() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001789{
1790 if (size() > 0)
1791 {
1792 __deallocate(__p1_.first().__next_);
1793 __p1_.first().__next_ = nullptr;
1794 size_type __bc = bucket_count();
Howard Hinnant1f8da842011-07-05 14:14:17 +00001795 for (size_type __i = 0; __i < __bc; ++__i)
Howard Hinnant3e519522010-05-11 19:42:16 +00001796 __bucket_list_[__i] = nullptr;
1797 size() = 0;
1798 }
1799}
1800
1801template <class _Tp, class _Hash, class _Equal, class _Alloc>
1802pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
1803__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_unique(__node_pointer __nd)
1804{
1805 __nd->__hash_ = hash_function()(__nd->__value_);
1806 size_type __bc = bucket_count();
1807 bool __inserted = false;
1808 __node_pointer __ndptr;
1809 size_t __chash;
1810 if (__bc != 0)
1811 {
Howard Hinnant4cb38a82012-07-06 17:31:14 +00001812 __chash = __constrain_hash(__nd->__hash_, __bc);
Howard Hinnant3e519522010-05-11 19:42:16 +00001813 __ndptr = __bucket_list_[__chash];
1814 if (__ndptr != nullptr)
1815 {
1816 for (__ndptr = __ndptr->__next_; __ndptr != nullptr &&
Howard Hinnant4cb38a82012-07-06 17:31:14 +00001817 __constrain_hash(__ndptr->__hash_, __bc) == __chash;
Howard Hinnant3e519522010-05-11 19:42:16 +00001818 __ndptr = __ndptr->__next_)
1819 {
1820 if (key_eq()(__ndptr->__value_, __nd->__value_))
1821 goto __done;
1822 }
1823 }
1824 }
1825 {
1826 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1827 {
Eric Fiselier9ba5c112015-02-02 21:31:48 +00001828 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnant3e519522010-05-11 19:42:16 +00001829 size_type(ceil(float(size() + 1) / max_load_factor()))));
1830 __bc = bucket_count();
Howard Hinnant4cb38a82012-07-06 17:31:14 +00001831 __chash = __constrain_hash(__nd->__hash_, __bc);
Howard Hinnant3e519522010-05-11 19:42:16 +00001832 }
1833 // insert_after __bucket_list_[__chash], or __first_node if bucket is null
1834 __node_pointer __pn = __bucket_list_[__chash];
1835 if (__pn == nullptr)
1836 {
Howard Hinnant307f8142013-06-22 15:21:29 +00001837 __pn = static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
Howard Hinnant3e519522010-05-11 19:42:16 +00001838 __nd->__next_ = __pn->__next_;
1839 __pn->__next_ = __nd;
1840 // fix up __bucket_list_
1841 __bucket_list_[__chash] = __pn;
1842 if (__nd->__next_ != nullptr)
Howard Hinnant4cb38a82012-07-06 17:31:14 +00001843 __bucket_list_[__constrain_hash(__nd->__next_->__hash_, __bc)] = __nd;
Howard Hinnant3e519522010-05-11 19:42:16 +00001844 }
1845 else
1846 {
1847 __nd->__next_ = __pn->__next_;
1848 __pn->__next_ = __nd;
1849 }
1850 __ndptr = __nd;
1851 // increment size
1852 ++size();
1853 __inserted = true;
1854 }
1855__done:
Howard Hinnantb24c8022013-07-23 22:01:58 +00001856#if _LIBCPP_DEBUG_LEVEL >= 2
1857 return pair<iterator, bool>(iterator(__ndptr, this), __inserted);
1858#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001859 return pair<iterator, bool>(iterator(__ndptr), __inserted);
Howard Hinnantb24c8022013-07-23 22:01:58 +00001860#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001861}
1862
1863template <class _Tp, class _Hash, class _Equal, class _Alloc>
1864typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1865__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi(__node_pointer __cp)
1866{
1867 __cp->__hash_ = hash_function()(__cp->__value_);
1868 size_type __bc = bucket_count();
1869 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1870 {
Eric Fiselier9ba5c112015-02-02 21:31:48 +00001871 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnant3e519522010-05-11 19:42:16 +00001872 size_type(ceil(float(size() + 1) / max_load_factor()))));
1873 __bc = bucket_count();
1874 }
Howard Hinnant4cb38a82012-07-06 17:31:14 +00001875 size_t __chash = __constrain_hash(__cp->__hash_, __bc);
Howard Hinnant3e519522010-05-11 19:42:16 +00001876 __node_pointer __pn = __bucket_list_[__chash];
1877 if (__pn == nullptr)
1878 {
Howard Hinnant307f8142013-06-22 15:21:29 +00001879 __pn = static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
Howard Hinnant3e519522010-05-11 19:42:16 +00001880 __cp->__next_ = __pn->__next_;
1881 __pn->__next_ = __cp;
1882 // fix up __bucket_list_
1883 __bucket_list_[__chash] = __pn;
1884 if (__cp->__next_ != nullptr)
Howard Hinnant4cb38a82012-07-06 17:31:14 +00001885 __bucket_list_[__constrain_hash(__cp->__next_->__hash_, __bc)] = __cp;
Howard Hinnant3e519522010-05-11 19:42:16 +00001886 }
1887 else
1888 {
1889 for (bool __found = false; __pn->__next_ != nullptr &&
Howard Hinnant4cb38a82012-07-06 17:31:14 +00001890 __constrain_hash(__pn->__next_->__hash_, __bc) == __chash;
Howard Hinnant3e519522010-05-11 19:42:16 +00001891 __pn = __pn->__next_)
Howard Hinnantb3371f62010-08-22 00:02:43 +00001892 {
Howard Hinnant3e519522010-05-11 19:42:16 +00001893 // __found key_eq() action
1894 // false false loop
1895 // true true loop
1896 // false true set __found to true
1897 // true false break
1898 if (__found != (__pn->__next_->__hash_ == __cp->__hash_ &&
1899 key_eq()(__pn->__next_->__value_, __cp->__value_)))
1900 {
1901 if (!__found)
1902 __found = true;
1903 else
1904 break;
1905 }
1906 }
1907 __cp->__next_ = __pn->__next_;
1908 __pn->__next_ = __cp;
1909 if (__cp->__next_ != nullptr)
1910 {
Howard Hinnant4cb38a82012-07-06 17:31:14 +00001911 size_t __nhash = __constrain_hash(__cp->__next_->__hash_, __bc);
Howard Hinnant3e519522010-05-11 19:42:16 +00001912 if (__nhash != __chash)
1913 __bucket_list_[__nhash] = __cp;
1914 }
1915 }
1916 ++size();
Howard Hinnantb24c8022013-07-23 22:01:58 +00001917#if _LIBCPP_DEBUG_LEVEL >= 2
1918 return iterator(__cp, this);
1919#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001920 return iterator(__cp);
Howard Hinnantb24c8022013-07-23 22:01:58 +00001921#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001922}
1923
1924template <class _Tp, class _Hash, class _Equal, class _Alloc>
1925typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1926__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi(
1927 const_iterator __p, __node_pointer __cp)
1928{
Howard Hinnant2f51de52013-08-02 17:50:49 +00001929#if _LIBCPP_DEBUG_LEVEL >= 2
1930 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1931 "unordered container::emplace_hint(const_iterator, args...) called with an iterator not"
1932 " referring to this unordered container");
1933#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001934 if (__p != end() && key_eq()(*__p, __cp->__value_))
1935 {
Howard Hinnant307f8142013-06-22 15:21:29 +00001936 __node_pointer __np = __p.__node_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001937 __cp->__hash_ = __np->__hash_;
1938 size_type __bc = bucket_count();
1939 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1940 {
Eric Fiselier9ba5c112015-02-02 21:31:48 +00001941 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnant3e519522010-05-11 19:42:16 +00001942 size_type(ceil(float(size() + 1) / max_load_factor()))));
1943 __bc = bucket_count();
1944 }
Howard Hinnant4cb38a82012-07-06 17:31:14 +00001945 size_t __chash = __constrain_hash(__cp->__hash_, __bc);
Howard Hinnant3e519522010-05-11 19:42:16 +00001946 __node_pointer __pp = __bucket_list_[__chash];
1947 while (__pp->__next_ != __np)
1948 __pp = __pp->__next_;
1949 __cp->__next_ = __np;
1950 __pp->__next_ = __cp;
1951 ++size();
Howard Hinnantb24c8022013-07-23 22:01:58 +00001952#if _LIBCPP_DEBUG_LEVEL >= 2
1953 return iterator(__cp, this);
1954#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001955 return iterator(__cp);
Howard Hinnantb24c8022013-07-23 22:01:58 +00001956#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001957 }
1958 return __node_insert_multi(__cp);
1959}
1960
Eric Fiselierde3f2b32015-06-13 07:18:32 +00001961
1962
Eric Fiselierfcd02212016-02-11 11:59:44 +00001963#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierde3f2b32015-06-13 07:18:32 +00001964template <class _Tp, class _Hash, class _Equal, class _Alloc>
Eric Fiselierfcd02212016-02-11 11:59:44 +00001965template <class _Key, class ..._Args>
Eric Fiselierde3f2b32015-06-13 07:18:32 +00001966_LIBCPP_INLINE_VISIBILITY
1967pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
Eric Fiselierfcd02212016-02-11 11:59:44 +00001968__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_key_args(_Key const& __k, _Args&&... __args)
Eric Fiselierde3f2b32015-06-13 07:18:32 +00001969#else
1970template <class _Tp, class _Hash, class _Equal, class _Alloc>
Eric Fiselierfcd02212016-02-11 11:59:44 +00001971template <class _Key, class _Args>
Eric Fiselierde3f2b32015-06-13 07:18:32 +00001972_LIBCPP_INLINE_VISIBILITY
1973pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
Eric Fiselierfcd02212016-02-11 11:59:44 +00001974__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_key_args(_Key const& __k, _Args& __args)
Eric Fiselierde3f2b32015-06-13 07:18:32 +00001975#endif
1976{
Eric Fiselierfcd02212016-02-11 11:59:44 +00001977
1978 size_t __hash = hash_function()(__k);
Howard Hinnant3e519522010-05-11 19:42:16 +00001979 size_type __bc = bucket_count();
1980 bool __inserted = false;
1981 __node_pointer __nd;
1982 size_t __chash;
1983 if (__bc != 0)
1984 {
Howard Hinnant4cb38a82012-07-06 17:31:14 +00001985 __chash = __constrain_hash(__hash, __bc);
Howard Hinnant3e519522010-05-11 19:42:16 +00001986 __nd = __bucket_list_[__chash];
1987 if (__nd != nullptr)
1988 {
1989 for (__nd = __nd->__next_; __nd != nullptr &&
Howard Hinnant4cb38a82012-07-06 17:31:14 +00001990 __constrain_hash(__nd->__hash_, __bc) == __chash;
Howard Hinnant3e519522010-05-11 19:42:16 +00001991 __nd = __nd->__next_)
1992 {
Eric Fiselierfcd02212016-02-11 11:59:44 +00001993 if (key_eq()(__nd->__value_, __k))
Howard Hinnant3e519522010-05-11 19:42:16 +00001994 goto __done;
1995 }
1996 }
1997 }
1998 {
Eric Fiselierfcd02212016-02-11 11:59:44 +00001999#ifndef _LIBCPP_CXX03_LANG
2000 __node_holder __h = __construct_node_hash(__hash, _VSTD::forward<_Args>(__args)...);
2001#else
2002 __node_holder __h = __construct_node_hash(__hash, __args);
2003#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002004 if (size()+1 > __bc * max_load_factor() || __bc == 0)
2005 {
Eric Fiselier9ba5c112015-02-02 21:31:48 +00002006 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnant3e519522010-05-11 19:42:16 +00002007 size_type(ceil(float(size() + 1) / max_load_factor()))));
2008 __bc = bucket_count();
Howard Hinnant4cb38a82012-07-06 17:31:14 +00002009 __chash = __constrain_hash(__hash, __bc);
Howard Hinnant3e519522010-05-11 19:42:16 +00002010 }
2011 // insert_after __bucket_list_[__chash], or __first_node if bucket is null
2012 __node_pointer __pn = __bucket_list_[__chash];
2013 if (__pn == nullptr)
2014 {
Eric Fiselier8e397682016-02-11 15:22:37 +00002015 __pn = static_cast<__node_pointer>(static_cast<__void_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first())));
Howard Hinnant3e519522010-05-11 19:42:16 +00002016 __h->__next_ = __pn->__next_;
2017 __pn->__next_ = __h.get();
2018 // fix up __bucket_list_
2019 __bucket_list_[__chash] = __pn;
2020 if (__h->__next_ != nullptr)
Howard Hinnant4cb38a82012-07-06 17:31:14 +00002021 __bucket_list_[__constrain_hash(__h->__next_->__hash_, __bc)] = __h.get();
Howard Hinnant3e519522010-05-11 19:42:16 +00002022 }
2023 else
2024 {
2025 __h->__next_ = __pn->__next_;
2026 __pn->__next_ = __h.get();
2027 }
2028 __nd = __h.release();
2029 // increment size
2030 ++size();
2031 __inserted = true;
2032 }
2033__done:
Howard Hinnantb24c8022013-07-23 22:01:58 +00002034#if _LIBCPP_DEBUG_LEVEL >= 2
2035 return pair<iterator, bool>(iterator(__nd, this), __inserted);
2036#else
Howard Hinnant3e519522010-05-11 19:42:16 +00002037 return pair<iterator, bool>(iterator(__nd), __inserted);
Howard Hinnantb24c8022013-07-23 22:01:58 +00002038#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002039}
2040
Eric Fiselierfcd02212016-02-11 11:59:44 +00002041#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00002042
2043template <class _Tp, class _Hash, class _Equal, class _Alloc>
2044template <class... _Args>
2045pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
Duncan P. N. Exon Smithfde79b42016-03-17 20:45:20 +00002046__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_impl(_Args&&... __args)
Howard Hinnant3e519522010-05-11 19:42:16 +00002047{
Howard Hinnantce48a112011-06-30 21:18:19 +00002048 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnant3e519522010-05-11 19:42:16 +00002049 pair<iterator, bool> __r = __node_insert_unique(__h.get());
2050 if (__r.second)
2051 __h.release();
2052 return __r;
2053}
2054
2055template <class _Tp, class _Hash, class _Equal, class _Alloc>
2056template <class... _Args>
2057typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2058__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_multi(_Args&&... __args)
2059{
Howard Hinnantce48a112011-06-30 21:18:19 +00002060 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnant3e519522010-05-11 19:42:16 +00002061 iterator __r = __node_insert_multi(__h.get());
2062 __h.release();
2063 return __r;
2064}
2065
2066template <class _Tp, class _Hash, class _Equal, class _Alloc>
2067template <class... _Args>
2068typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2069__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_hint_multi(
2070 const_iterator __p, _Args&&... __args)
2071{
Howard Hinnante5c13de2013-07-29 19:05:47 +00002072#if _LIBCPP_DEBUG_LEVEL >= 2
2073 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2074 "unordered container::emplace_hint(const_iterator, args...) called with an iterator not"
2075 " referring to this unordered container");
2076#endif
Howard Hinnantce48a112011-06-30 21:18:19 +00002077 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnant3e519522010-05-11 19:42:16 +00002078 iterator __r = __node_insert_multi(__p, __h.get());
2079 __h.release();
2080 return __r;
2081}
2082
Eric Fiselierfcd02212016-02-11 11:59:44 +00002083#else // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00002084
2085template <class _Tp, class _Hash, class _Equal, class _Alloc>
2086typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
Eric Fiselierfcd02212016-02-11 11:59:44 +00002087__hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const __container_value_type& __x)
Howard Hinnant3e519522010-05-11 19:42:16 +00002088{
2089 __node_holder __h = __construct_node(__x);
2090 iterator __r = __node_insert_multi(__h.get());
2091 __h.release();
2092 return __r;
2093}
2094
2095template <class _Tp, class _Hash, class _Equal, class _Alloc>
2096typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2097__hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const_iterator __p,
Eric Fiselierfcd02212016-02-11 11:59:44 +00002098 const __container_value_type& __x)
Howard Hinnant3e519522010-05-11 19:42:16 +00002099{
Howard Hinnante5c13de2013-07-29 19:05:47 +00002100#if _LIBCPP_DEBUG_LEVEL >= 2
2101 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2102 "unordered container::insert(const_iterator, lvalue) called with an iterator not"
2103 " referring to this unordered container");
2104#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002105 __node_holder __h = __construct_node(__x);
2106 iterator __r = __node_insert_multi(__p, __h.get());
2107 __h.release();
2108 return __r;
2109}
2110
Eric Fiselierfcd02212016-02-11 11:59:44 +00002111#endif // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00002112
2113template <class _Tp, class _Hash, class _Equal, class _Alloc>
2114void
2115__hash_table<_Tp, _Hash, _Equal, _Alloc>::rehash(size_type __n)
2116{
Howard Hinnant4cb38a82012-07-06 17:31:14 +00002117 if (__n == 1)
2118 __n = 2;
2119 else if (__n & (__n - 1))
2120 __n = __next_prime(__n);
Howard Hinnant3e519522010-05-11 19:42:16 +00002121 size_type __bc = bucket_count();
2122 if (__n > __bc)
2123 __rehash(__n);
Howard Hinnant4cb38a82012-07-06 17:31:14 +00002124 else if (__n < __bc)
Howard Hinnant3e519522010-05-11 19:42:16 +00002125 {
Howard Hinnantce48a112011-06-30 21:18:19 +00002126 __n = _VSTD::max<size_type>
Howard Hinnant3e519522010-05-11 19:42:16 +00002127 (
2128 __n,
Eric Fiselier9ba5c112015-02-02 21:31:48 +00002129 __is_hash_power2(__bc) ? __next_hash_pow2(size_t(ceil(float(size()) / max_load_factor()))) :
2130 __next_prime(size_t(ceil(float(size()) / max_load_factor())))
Howard Hinnant3e519522010-05-11 19:42:16 +00002131 );
2132 if (__n < __bc)
2133 __rehash(__n);
2134 }
2135}
2136
2137template <class _Tp, class _Hash, class _Equal, class _Alloc>
2138void
2139__hash_table<_Tp, _Hash, _Equal, _Alloc>::__rehash(size_type __nbc)
2140{
Howard Hinnantb24c8022013-07-23 22:01:58 +00002141#if _LIBCPP_DEBUG_LEVEL >= 2
2142 __get_db()->__invalidate_all(this);
2143#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant3e519522010-05-11 19:42:16 +00002144 __pointer_allocator& __npa = __bucket_list_.get_deleter().__alloc();
2145 __bucket_list_.reset(__nbc > 0 ?
2146 __pointer_alloc_traits::allocate(__npa, __nbc) : nullptr);
2147 __bucket_list_.get_deleter().size() = __nbc;
2148 if (__nbc > 0)
2149 {
2150 for (size_type __i = 0; __i < __nbc; ++__i)
2151 __bucket_list_[__i] = nullptr;
Howard Hinnant307f8142013-06-22 15:21:29 +00002152 __node_pointer __pp(static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first())));
Howard Hinnant3e519522010-05-11 19:42:16 +00002153 __node_pointer __cp = __pp->__next_;
2154 if (__cp != nullptr)
2155 {
Howard Hinnant4cb38a82012-07-06 17:31:14 +00002156 size_type __chash = __constrain_hash(__cp->__hash_, __nbc);
Howard Hinnant3e519522010-05-11 19:42:16 +00002157 __bucket_list_[__chash] = __pp;
2158 size_type __phash = __chash;
2159 for (__pp = __cp, __cp = __cp->__next_; __cp != nullptr;
2160 __cp = __pp->__next_)
2161 {
Howard Hinnant4cb38a82012-07-06 17:31:14 +00002162 __chash = __constrain_hash(__cp->__hash_, __nbc);
Howard Hinnant3e519522010-05-11 19:42:16 +00002163 if (__chash == __phash)
2164 __pp = __cp;
2165 else
2166 {
2167 if (__bucket_list_[__chash] == nullptr)
2168 {
2169 __bucket_list_[__chash] = __pp;
2170 __pp = __cp;
2171 __phash = __chash;
2172 }
2173 else
2174 {
2175 __node_pointer __np = __cp;
2176 for (; __np->__next_ != nullptr &&
2177 key_eq()(__cp->__value_, __np->__next_->__value_);
2178 __np = __np->__next_)
2179 ;
2180 __pp->__next_ = __np->__next_;
2181 __np->__next_ = __bucket_list_[__chash]->__next_;
2182 __bucket_list_[__chash]->__next_ = __cp;
Howard Hinnantb3371f62010-08-22 00:02:43 +00002183
Howard Hinnant3e519522010-05-11 19:42:16 +00002184 }
2185 }
2186 }
2187 }
2188 }
2189}
2190
2191template <class _Tp, class _Hash, class _Equal, class _Alloc>
2192template <class _Key>
2193typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2194__hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k)
2195{
2196 size_t __hash = hash_function()(__k);
2197 size_type __bc = bucket_count();
2198 if (__bc != 0)
2199 {
Howard Hinnant4cb38a82012-07-06 17:31:14 +00002200 size_t __chash = __constrain_hash(__hash, __bc);
Howard Hinnant3e519522010-05-11 19:42:16 +00002201 __node_pointer __nd = __bucket_list_[__chash];
2202 if (__nd != nullptr)
2203 {
2204 for (__nd = __nd->__next_; __nd != nullptr &&
Eric Fiselier6a411472016-07-17 22:04:57 +00002205 (__nd->__hash_ == __hash
2206 || __constrain_hash(__nd->__hash_, __bc) == __chash);
Howard Hinnant3e519522010-05-11 19:42:16 +00002207 __nd = __nd->__next_)
2208 {
Kwasi Mensah318d35a2016-07-08 15:34:28 +00002209 if ((__nd->__hash_ == __hash) && key_eq()(__nd->__value_, __k))
Howard Hinnantb24c8022013-07-23 22:01:58 +00002210#if _LIBCPP_DEBUG_LEVEL >= 2
2211 return iterator(__nd, this);
2212#else
Howard Hinnant3e519522010-05-11 19:42:16 +00002213 return iterator(__nd);
Howard Hinnantb24c8022013-07-23 22:01:58 +00002214#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002215 }
2216 }
2217 }
2218 return end();
2219}
2220
2221template <class _Tp, class _Hash, class _Equal, class _Alloc>
2222template <class _Key>
2223typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
2224__hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k) const
2225{
2226 size_t __hash = hash_function()(__k);
2227 size_type __bc = bucket_count();
2228 if (__bc != 0)
2229 {
Howard Hinnant4cb38a82012-07-06 17:31:14 +00002230 size_t __chash = __constrain_hash(__hash, __bc);
Howard Hinnant3e519522010-05-11 19:42:16 +00002231 __node_const_pointer __nd = __bucket_list_[__chash];
2232 if (__nd != nullptr)
2233 {
2234 for (__nd = __nd->__next_; __nd != nullptr &&
Eric Fiselier6a411472016-07-17 22:04:57 +00002235 (__hash == __nd->__hash_ || __constrain_hash(__nd->__hash_, __bc) == __chash);
Howard Hinnant3e519522010-05-11 19:42:16 +00002236 __nd = __nd->__next_)
2237 {
Kwasi Mensah318d35a2016-07-08 15:34:28 +00002238 if ((__nd->__hash_ == __hash) && key_eq()(__nd->__value_, __k))
Howard Hinnantb24c8022013-07-23 22:01:58 +00002239#if _LIBCPP_DEBUG_LEVEL >= 2
2240 return const_iterator(__nd, this);
2241#else
Howard Hinnant3e519522010-05-11 19:42:16 +00002242 return const_iterator(__nd);
Howard Hinnantb24c8022013-07-23 22:01:58 +00002243#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002244 }
2245 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00002246
Howard Hinnant3e519522010-05-11 19:42:16 +00002247 }
2248 return end();
2249}
2250
Eric Fiselierfcd02212016-02-11 11:59:44 +00002251#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00002252
2253template <class _Tp, class _Hash, class _Equal, class _Alloc>
2254template <class ..._Args>
2255typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
2256__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(_Args&& ...__args)
2257{
Eric Fiselierfcd02212016-02-11 11:59:44 +00002258 static_assert(!__is_hash_value_type<_Args...>::value,
2259 "Construct cannot be called with a hash value type");
Howard Hinnant3e519522010-05-11 19:42:16 +00002260 __node_allocator& __na = __node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00002261 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselierfcd02212016-02-11 11:59:44 +00002262 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), _VSTD::forward<_Args>(__args)...);
Howard Hinnant3e519522010-05-11 19:42:16 +00002263 __h.get_deleter().__value_constructed = true;
2264 __h->__hash_ = hash_function()(__h->__value_);
2265 __h->__next_ = nullptr;
2266 return __h;
2267}
2268
2269template <class _Tp, class _Hash, class _Equal, class _Alloc>
Eric Fiselierfcd02212016-02-11 11:59:44 +00002270template <class _First, class ..._Rest>
Howard Hinnant3e519522010-05-11 19:42:16 +00002271typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Eric Fiselierfcd02212016-02-11 11:59:44 +00002272__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node_hash(
2273 size_t __hash, _First&& __f, _Rest&& ...__rest)
Howard Hinnant3e519522010-05-11 19:42:16 +00002274{
Eric Fiselierfcd02212016-02-11 11:59:44 +00002275 static_assert(!__is_hash_value_type<_First, _Rest...>::value,
2276 "Construct cannot be called with a hash value type");
Howard Hinnant3e519522010-05-11 19:42:16 +00002277 __node_allocator& __na = __node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00002278 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselierfcd02212016-02-11 11:59:44 +00002279 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_),
2280 _VSTD::forward<_First>(__f),
2281 _VSTD::forward<_Rest>(__rest)...);
Howard Hinnant3e519522010-05-11 19:42:16 +00002282 __h.get_deleter().__value_constructed = true;
2283 __h->__hash_ = __hash;
2284 __h->__next_ = nullptr;
Howard Hinnant179b1f82013-08-22 18:29:50 +00002285 return __h;
Howard Hinnant3e519522010-05-11 19:42:16 +00002286}
2287
Eric Fiselierfcd02212016-02-11 11:59:44 +00002288#else // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00002289
2290template <class _Tp, class _Hash, class _Equal, class _Alloc>
2291typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Eric Fiselierfcd02212016-02-11 11:59:44 +00002292__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(const __container_value_type& __v)
Howard Hinnant3e519522010-05-11 19:42:16 +00002293{
2294 __node_allocator& __na = __node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00002295 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselierfcd02212016-02-11 11:59:44 +00002296 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), __v);
Howard Hinnant3e519522010-05-11 19:42:16 +00002297 __h.get_deleter().__value_constructed = true;
2298 __h->__hash_ = hash_function()(__h->__value_);
2299 __h->__next_ = nullptr;
Dimitry Andric251c6292015-08-19 06:43:33 +00002300 return _LIBCPP_EXPLICIT_MOVE(__h); // explicitly moved for C++03
Howard Hinnant3e519522010-05-11 19:42:16 +00002301}
2302
Howard Hinnant3e519522010-05-11 19:42:16 +00002303template <class _Tp, class _Hash, class _Equal, class _Alloc>
2304typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Eric Fiselierfcd02212016-02-11 11:59:44 +00002305__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node_hash(size_t __hash,
2306 const __container_value_type& __v)
Howard Hinnant3e519522010-05-11 19:42:16 +00002307{
2308 __node_allocator& __na = __node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00002309 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselierfcd02212016-02-11 11:59:44 +00002310 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), __v);
Howard Hinnant3e519522010-05-11 19:42:16 +00002311 __h.get_deleter().__value_constructed = true;
2312 __h->__hash_ = __hash;
2313 __h->__next_ = nullptr;
Dimitry Andric251c6292015-08-19 06:43:33 +00002314 return _LIBCPP_EXPLICIT_MOVE(__h); // explicitly moved for C++03
Howard Hinnant3e519522010-05-11 19:42:16 +00002315}
2316
Eric Fiselierfcd02212016-02-11 11:59:44 +00002317#endif // _LIBCPP_CXX03_LANG
2318
Howard Hinnant3e519522010-05-11 19:42:16 +00002319template <class _Tp, class _Hash, class _Equal, class _Alloc>
2320typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2321__hash_table<_Tp, _Hash, _Equal, _Alloc>::erase(const_iterator __p)
2322{
Howard Hinnant307f8142013-06-22 15:21:29 +00002323 __node_pointer __np = __p.__node_;
Howard Hinnantb24c8022013-07-23 22:01:58 +00002324#if _LIBCPP_DEBUG_LEVEL >= 2
2325 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2326 "unordered container erase(iterator) called with an iterator not"
2327 " referring to this container");
2328 _LIBCPP_ASSERT(__p != end(),
2329 "unordered container erase(iterator) called with a non-dereferenceable iterator");
2330 iterator __r(__np, this);
2331#else
Howard Hinnant3e519522010-05-11 19:42:16 +00002332 iterator __r(__np);
Howard Hinnantb24c8022013-07-23 22:01:58 +00002333#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002334 ++__r;
2335 remove(__p);
2336 return __r;
2337}
2338
2339template <class _Tp, class _Hash, class _Equal, class _Alloc>
2340typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2341__hash_table<_Tp, _Hash, _Equal, _Alloc>::erase(const_iterator __first,
2342 const_iterator __last)
2343{
Howard Hinnantb24c8022013-07-23 22:01:58 +00002344#if _LIBCPP_DEBUG_LEVEL >= 2
2345 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
2346 "unodered container::erase(iterator, iterator) called with an iterator not"
2347 " referring to this unodered container");
2348 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__last) == this,
2349 "unodered container::erase(iterator, iterator) called with an iterator not"
2350 " referring to this unodered container");
2351#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002352 for (const_iterator __p = __first; __first != __last; __p = __first)
2353 {
2354 ++__first;
2355 erase(__p);
2356 }
Howard Hinnant307f8142013-06-22 15:21:29 +00002357 __node_pointer __np = __last.__node_;
Howard Hinnantb24c8022013-07-23 22:01:58 +00002358#if _LIBCPP_DEBUG_LEVEL >= 2
2359 return iterator (__np, this);
2360#else
Howard Hinnant3e519522010-05-11 19:42:16 +00002361 return iterator (__np);
Howard Hinnantb24c8022013-07-23 22:01:58 +00002362#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002363}
2364
2365template <class _Tp, class _Hash, class _Equal, class _Alloc>
2366template <class _Key>
2367typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2368__hash_table<_Tp, _Hash, _Equal, _Alloc>::__erase_unique(const _Key& __k)
2369{
2370 iterator __i = find(__k);
2371 if (__i == end())
2372 return 0;
2373 erase(__i);
2374 return 1;
2375}
2376
2377template <class _Tp, class _Hash, class _Equal, class _Alloc>
2378template <class _Key>
2379typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2380__hash_table<_Tp, _Hash, _Equal, _Alloc>::__erase_multi(const _Key& __k)
2381{
2382 size_type __r = 0;
2383 iterator __i = find(__k);
2384 if (__i != end())
2385 {
2386 iterator __e = end();
2387 do
2388 {
2389 erase(__i++);
2390 ++__r;
2391 } while (__i != __e && key_eq()(*__i, __k));
2392 }
2393 return __r;
2394}
2395
2396template <class _Tp, class _Hash, class _Equal, class _Alloc>
2397typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Howard Hinnant37141072011-06-04 18:54:24 +00002398__hash_table<_Tp, _Hash, _Equal, _Alloc>::remove(const_iterator __p) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00002399{
2400 // current node
Howard Hinnant307f8142013-06-22 15:21:29 +00002401 __node_pointer __cn = __p.__node_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002402 size_type __bc = bucket_count();
Howard Hinnant4cb38a82012-07-06 17:31:14 +00002403 size_t __chash = __constrain_hash(__cn->__hash_, __bc);
Howard Hinnant3e519522010-05-11 19:42:16 +00002404 // find previous node
2405 __node_pointer __pn = __bucket_list_[__chash];
2406 for (; __pn->__next_ != __cn; __pn = __pn->__next_)
2407 ;
2408 // Fix up __bucket_list_
2409 // if __pn is not in same bucket (before begin is not in same bucket) &&
2410 // if __cn->__next_ is not in same bucket (nullptr is not in same bucket)
Howard Hinnant307f8142013-06-22 15:21:29 +00002411 if (__pn == static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()))
2412 || __constrain_hash(__pn->__hash_, __bc) != __chash)
Howard Hinnant3e519522010-05-11 19:42:16 +00002413 {
Howard Hinnant4cb38a82012-07-06 17:31:14 +00002414 if (__cn->__next_ == nullptr || __constrain_hash(__cn->__next_->__hash_, __bc) != __chash)
Howard Hinnant3e519522010-05-11 19:42:16 +00002415 __bucket_list_[__chash] = nullptr;
2416 }
2417 // if __cn->__next_ is not in same bucket (nullptr is in same bucket)
2418 if (__cn->__next_ != nullptr)
2419 {
Howard Hinnant4cb38a82012-07-06 17:31:14 +00002420 size_t __nhash = __constrain_hash(__cn->__next_->__hash_, __bc);
Howard Hinnant3e519522010-05-11 19:42:16 +00002421 if (__nhash != __chash)
2422 __bucket_list_[__nhash] = __pn;
2423 }
2424 // remove __cn
2425 __pn->__next_ = __cn->__next_;
2426 __cn->__next_ = nullptr;
2427 --size();
Howard Hinnantb24c8022013-07-23 22:01:58 +00002428#if _LIBCPP_DEBUG_LEVEL >= 2
2429 __c_node* __c = __get_db()->__find_c_and_lock(this);
2430 for (__i_node** __p = __c->end_; __p != __c->beg_; )
2431 {
2432 --__p;
2433 iterator* __i = static_cast<iterator*>((*__p)->__i_);
2434 if (__i->__node_ == __cn)
2435 {
2436 (*__p)->__c_ = nullptr;
2437 if (--__c->end_ != __p)
2438 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
2439 }
2440 }
2441 __get_db()->unlock();
2442#endif
Howard Hinnantc003db12011-11-29 18:15:50 +00002443 return __node_holder(__cn, _Dp(__node_alloc(), true));
Howard Hinnant3e519522010-05-11 19:42:16 +00002444}
2445
2446template <class _Tp, class _Hash, class _Equal, class _Alloc>
2447template <class _Key>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00002448inline
Howard Hinnant3e519522010-05-11 19:42:16 +00002449typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2450__hash_table<_Tp, _Hash, _Equal, _Alloc>::__count_unique(const _Key& __k) const
2451{
2452 return static_cast<size_type>(find(__k) != end());
2453}
2454
2455template <class _Tp, class _Hash, class _Equal, class _Alloc>
2456template <class _Key>
2457typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2458__hash_table<_Tp, _Hash, _Equal, _Alloc>::__count_multi(const _Key& __k) const
2459{
2460 size_type __r = 0;
2461 const_iterator __i = find(__k);
2462 if (__i != end())
2463 {
2464 const_iterator __e = end();
2465 do
2466 {
2467 ++__i;
2468 ++__r;
2469 } while (__i != __e && key_eq()(*__i, __k));
2470 }
2471 return __r;
2472}
2473
2474template <class _Tp, class _Hash, class _Equal, class _Alloc>
2475template <class _Key>
2476pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator,
2477 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator>
2478__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_unique(
2479 const _Key& __k)
2480{
2481 iterator __i = find(__k);
2482 iterator __j = __i;
2483 if (__i != end())
2484 ++__j;
2485 return pair<iterator, iterator>(__i, __j);
2486}
2487
2488template <class _Tp, class _Hash, class _Equal, class _Alloc>
2489template <class _Key>
2490pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator,
2491 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator>
2492__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_unique(
2493 const _Key& __k) const
2494{
2495 const_iterator __i = find(__k);
2496 const_iterator __j = __i;
2497 if (__i != end())
2498 ++__j;
2499 return pair<const_iterator, const_iterator>(__i, __j);
2500}
2501
2502template <class _Tp, class _Hash, class _Equal, class _Alloc>
2503template <class _Key>
2504pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator,
2505 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator>
2506__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_multi(
2507 const _Key& __k)
2508{
2509 iterator __i = find(__k);
2510 iterator __j = __i;
2511 if (__i != end())
2512 {
2513 iterator __e = end();
2514 do
2515 {
2516 ++__j;
2517 } while (__j != __e && key_eq()(*__j, __k));
2518 }
2519 return pair<iterator, iterator>(__i, __j);
2520}
2521
2522template <class _Tp, class _Hash, class _Equal, class _Alloc>
2523template <class _Key>
2524pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator,
2525 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator>
2526__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_multi(
2527 const _Key& __k) const
2528{
2529 const_iterator __i = find(__k);
2530 const_iterator __j = __i;
2531 if (__i != end())
2532 {
2533 const_iterator __e = end();
2534 do
2535 {
2536 ++__j;
2537 } while (__j != __e && key_eq()(*__j, __k));
2538 }
2539 return pair<const_iterator, const_iterator>(__i, __j);
2540}
2541
2542template <class _Tp, class _Hash, class _Equal, class _Alloc>
2543void
2544__hash_table<_Tp, _Hash, _Equal, _Alloc>::swap(__hash_table& __u)
Eric Fiselier87a82492015-07-18 20:40:46 +00002545#if _LIBCPP_STD_VER <= 11
Howard Hinnant37141072011-06-04 18:54:24 +00002546 _NOEXCEPT_(
Marshall Clowe3fbe142015-07-13 20:04:56 +00002547 __is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value
Marshall Clowe3fbe142015-07-13 20:04:56 +00002548 && (!allocator_traits<__pointer_allocator>::propagate_on_container_swap::value
2549 || __is_nothrow_swappable<__pointer_allocator>::value)
2550 && (!__node_traits::propagate_on_container_swap::value
2551 || __is_nothrow_swappable<__node_allocator>::value)
Marshall Clowe3fbe142015-07-13 20:04:56 +00002552 )
Eric Fiselier87a82492015-07-18 20:40:46 +00002553#else
2554 _NOEXCEPT_(__is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value)
2555#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002556{
2557 {
2558 __node_pointer_pointer __npp = __bucket_list_.release();
2559 __bucket_list_.reset(__u.__bucket_list_.release());
2560 __u.__bucket_list_.reset(__npp);
2561 }
Howard Hinnantce48a112011-06-30 21:18:19 +00002562 _VSTD::swap(__bucket_list_.get_deleter().size(), __u.__bucket_list_.get_deleter().size());
Marshall Clowe3fbe142015-07-13 20:04:56 +00002563 __swap_allocator(__bucket_list_.get_deleter().__alloc(),
Howard Hinnant3e519522010-05-11 19:42:16 +00002564 __u.__bucket_list_.get_deleter().__alloc());
Marshall Clowe3fbe142015-07-13 20:04:56 +00002565 __swap_allocator(__node_alloc(), __u.__node_alloc());
Howard Hinnantce48a112011-06-30 21:18:19 +00002566 _VSTD::swap(__p1_.first().__next_, __u.__p1_.first().__next_);
Howard Hinnant3e519522010-05-11 19:42:16 +00002567 __p2_.swap(__u.__p2_);
2568 __p3_.swap(__u.__p3_);
2569 if (size() > 0)
Howard Hinnant4cb38a82012-07-06 17:31:14 +00002570 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash_, bucket_count())] =
Howard Hinnant307f8142013-06-22 15:21:29 +00002571 static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
Howard Hinnant3e519522010-05-11 19:42:16 +00002572 if (__u.size() > 0)
Howard Hinnant4cb38a82012-07-06 17:31:14 +00002573 __u.__bucket_list_[__constrain_hash(__u.__p1_.first().__next_->__hash_, __u.bucket_count())] =
Howard Hinnant307f8142013-06-22 15:21:29 +00002574 static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__u.__p1_.first()));
Howard Hinnantb24c8022013-07-23 22:01:58 +00002575#if _LIBCPP_DEBUG_LEVEL >= 2
2576 __get_db()->swap(this, &__u);
2577#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002578}
2579
2580template <class _Tp, class _Hash, class _Equal, class _Alloc>
2581typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2582__hash_table<_Tp, _Hash, _Equal, _Alloc>::bucket_size(size_type __n) const
2583{
Howard Hinnante5c13de2013-07-29 19:05:47 +00002584 _LIBCPP_ASSERT(__n < bucket_count(),
2585 "unordered container::bucket_size(n) called with n >= bucket_count()");
Howard Hinnant3e519522010-05-11 19:42:16 +00002586 __node_const_pointer __np = __bucket_list_[__n];
2587 size_type __bc = bucket_count();
2588 size_type __r = 0;
2589 if (__np != nullptr)
2590 {
2591 for (__np = __np->__next_; __np != nullptr &&
Howard Hinnant4cb38a82012-07-06 17:31:14 +00002592 __constrain_hash(__np->__hash_, __bc) == __n;
Howard Hinnant3e519522010-05-11 19:42:16 +00002593 __np = __np->__next_, ++__r)
2594 ;
2595 }
2596 return __r;
2597}
2598
Howard Hinnant37141072011-06-04 18:54:24 +00002599template <class _Tp, class _Hash, class _Equal, class _Alloc>
2600inline _LIBCPP_INLINE_VISIBILITY
2601void
2602swap(__hash_table<_Tp, _Hash, _Equal, _Alloc>& __x,
2603 __hash_table<_Tp, _Hash, _Equal, _Alloc>& __y)
2604 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
2605{
2606 __x.swap(__y);
2607}
2608
Howard Hinnantb24c8022013-07-23 22:01:58 +00002609#if _LIBCPP_DEBUG_LEVEL >= 2
2610
2611template <class _Tp, class _Hash, class _Equal, class _Alloc>
2612bool
2613__hash_table<_Tp, _Hash, _Equal, _Alloc>::__dereferenceable(const const_iterator* __i) const
2614{
2615 return __i->__node_ != nullptr;
2616}
2617
2618template <class _Tp, class _Hash, class _Equal, class _Alloc>
2619bool
2620__hash_table<_Tp, _Hash, _Equal, _Alloc>::__decrementable(const const_iterator*) const
2621{
2622 return false;
2623}
2624
2625template <class _Tp, class _Hash, class _Equal, class _Alloc>
2626bool
2627__hash_table<_Tp, _Hash, _Equal, _Alloc>::__addable(const const_iterator*, ptrdiff_t) const
2628{
2629 return false;
2630}
2631
2632template <class _Tp, class _Hash, class _Equal, class _Alloc>
2633bool
2634__hash_table<_Tp, _Hash, _Equal, _Alloc>::__subscriptable(const const_iterator*, ptrdiff_t) const
2635{
2636 return false;
2637}
2638
2639#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant3e519522010-05-11 19:42:16 +00002640_LIBCPP_END_NAMESPACE_STD
2641
2642#endif // _LIBCPP__HASH_TABLE