blob: 08bc519ae17f20a231e172889851eb31a74ec49d [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:161// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:014// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:165//
Howard Hinnantb64f8b02010-11-16 22:09:026// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:168//
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 Fiselier774c7c52016-02-10 20:46:2320#include <utility>
Howard Hinnantbc8d3f92010-05-11 19:42:1621
Howard Hinnant66c6f972011-11-29 16:45:2722#include <__undef_min_max>
Saleem Abdulrasoolf1b30c42015-02-13 22:15:3223#include <__undef___deallocate>
Howard Hinnant66c6f972011-11-29 16:45:2724
Eric Fiselierb9536102014-08-10 23:53:0825#include <__debug>
Howard Hinnant8b00e6c2013-08-02 00:26:3526
Howard Hinnant08e17472011-10-17 20:05:1027#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:1628#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:1029#endif
Howard Hinnantbc8d3f92010-05-11 19:42:1630
31_LIBCPP_BEGIN_NAMESPACE_STD
32
Eric Fiselier2960ae22016-02-11 11:59:4433
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 Hinnant83eade62013-03-06 23:30:1956_LIBCPP_FUNC_VIS
Howard Hinnant2b1b2d42011-06-14 19:58:1757size_t __next_prime(size_t __n);
Howard Hinnantbc8d3f92010-05-11 19:42:1658
59template <class _NodePtr>
60struct __hash_node_base
61{
62 typedef __hash_node_base __first_node;
Howard Hinnantbc8d3f92010-05-11 19:42:1663
Howard Hinnantdf85e572011-02-27 18:02:0264 _NodePtr __next_;
Howard Hinnantbc8d3f92010-05-11 19:42:1665
Howard Hinnant5f2f14c2011-06-04 18:54:2466 _LIBCPP_INLINE_VISIBILITY __hash_node_base() _NOEXCEPT : __next_(nullptr) {}
Howard Hinnantbc8d3f92010-05-11 19:42:1667};
68
69template <class _Tp, class _VoidPtr>
70struct __hash_node
71 : public __hash_node_base
72 <
Eric Fiselier5cf84e02015-12-30 21:52:0073 typename __rebind_pointer<_VoidPtr, __hash_node<_Tp, _VoidPtr> >::type
Howard Hinnantbc8d3f92010-05-11 19:42:1674 >
75{
Eric Fiselier774c7c52016-02-10 20:46:2376 typedef _Tp __node_value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:1677
78 size_t __hash_;
Eric Fiselier774c7c52016-02-10 20:46:2379 __node_value_type __value_;
Howard Hinnantbc8d3f92010-05-11 19:42:1680};
81
Howard Hinnant7a445152012-07-06 17:31:1482inline _LIBCPP_INLINE_VISIBILITY
83bool
Eric Fiselier57947ca2015-02-02 21:31:4884__is_hash_power2(size_t __bc)
Howard Hinnant7a445152012-07-06 17:31:1485{
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 Fiselier57663912016-07-11 22:02:0293 return !(__bc & (__bc - 1)) ? __h & (__bc - 1) :
94 (__h < __bc ? __h : __h % __bc);
Howard Hinnant7a445152012-07-06 17:31:1495}
96
97inline _LIBCPP_INLINE_VISIBILITY
98size_t
Eric Fiselier57947ca2015-02-02 21:31:4899__next_hash_pow2(size_t __n)
Howard Hinnant7a445152012-07-06 17:31:14100{
101 return size_t(1) << (std::numeric_limits<size_t>::digits - __clz(__n-1));
102}
103
Duncan P. N. Exon Smith798ec842016-03-17 20:45:20104
Howard Hinnant2b1b2d42011-06-14 19:58:17105template <class _Tp, class _Hash, class _Equal, class _Alloc> class __hash_table;
Eric Fiselier774c7c52016-02-10 20:46:23106
107template <class _NodePtr> class _LIBCPP_TYPE_VIS_ONLY __hash_iterator;
Howard Hinnant0f678bd2013-08-12 18:38:34108template <class _ConstNodePtr> class _LIBCPP_TYPE_VIS_ONLY __hash_const_iterator;
Eric Fiselier774c7c52016-02-10 20:46:23109template <class _NodePtr> class _LIBCPP_TYPE_VIS_ONLY __hash_local_iterator;
110template <class _ConstNodePtr> class _LIBCPP_TYPE_VIS_ONLY __hash_const_local_iterator;
Howard Hinnant0f678bd2013-08-12 18:38:34111template <class _HashIterator> class _LIBCPP_TYPE_VIS_ONLY __hash_map_iterator;
112template <class _HashIterator> class _LIBCPP_TYPE_VIS_ONLY __hash_map_const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16113
Eric Fiselier774c7c52016-02-10 20:46:23114template <class _Tp>
Eric Fiselier66e344f2016-02-20 07:59:16115struct __hash_key_value_types {
Eric Fiselier774c7c52016-02-10 20:46:23116 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 Fiselier2960ae22016-02-11 11:59:44121
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 Fiselier774c7c52016-02-10 20:46:23140};
141
142template <class _Key, class _Tp>
Eric Fiselier66e344f2016-02-20 07:59:16143struct __hash_key_value_types<__hash_value_type<_Key, _Tp> > {
Eric Fiselier774c7c52016-02-10 20:46:23144 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 Fiselier2960ae22016-02-11 11:59:44148 typedef pair<_Key, _Tp> __nc_value_type;
Eric Fiselier774c7c52016-02-10 20:46:23149 typedef __container_value_type __map_value_type;
150 static const bool __is_map = true;
Eric Fiselier2960ae22016-02-11 11:59:44151
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 Fiselier774c7c52016-02-10 20:46:23184};
185
Eric Fiselier66e344f2016-02-20 07:59:16186template <class _Tp, class _AllocPtr, class _KVTypes = __hash_key_value_types<_Tp>,
Eric Fiselier774c7c52016-02-10 20:46:23187 bool = _KVTypes::__is_map>
Eric Fiselier66e344f2016-02-20 07:59:16188struct __hash_map_pointer_types {};
Eric Fiselier774c7c52016-02-10 20:46:23189
190template <class _Tp, class _AllocPtr, class _KVTypes>
Eric Fiselier66e344f2016-02-20 07:59:16191struct __hash_map_pointer_types<_Tp, _AllocPtr, _KVTypes, true> {
Eric Fiselier774c7c52016-02-10 20:46:23192 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 Fiselier66e344f2016-02-20 07:59:16204 : public __hash_key_value_types<_Tp>, __hash_map_pointer_types<_Tp, _VoidPtr>
Eric Fiselier774c7c52016-02-10 20:46:23205
206{
Eric Fiselier66e344f2016-02-20 07:59:16207 typedef __hash_key_value_types<_Tp> __base;
Eric Fiselier774c7c52016-02-10 20:46:23208
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 Hinnantbc8d3f92010-05-11 19:42:16257template <class _NodePtr>
Howard Hinnant0f678bd2013-08-12 18:38:34258class _LIBCPP_TYPE_VIS_ONLY __hash_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16259{
Eric Fiselier774c7c52016-02-10 20:46:23260 typedef __hash_node_types<_NodePtr> _NodeTypes;
261 typedef _NodePtr __node_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16262
263 __node_pointer __node_;
264
265public:
Eric Fiselier774c7c52016-02-10 20:46:23266 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 Hinnantbc8d3f92010-05-11 19:42:16271
Howard Hinnant39213642013-07-23 22:01:58272 _LIBCPP_INLINE_VISIBILITY __hash_iterator() _NOEXCEPT
Marshall Clow193ef032013-08-07 21:30:44273#if _LIBCPP_STD_VER > 11
274 : __node_(nullptr)
275#endif
Howard Hinnant39213642013-07-23 22:01:58276 {
277#if _LIBCPP_DEBUG_LEVEL >= 2
278 __get_db()->__insert_i(this);
279#endif
280 }
281
282#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16283
Howard Hinnant99acc502010-09-21 17:32:39284 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58285 __hash_iterator(const __hash_iterator& __i)
286 : __node_(__i.__node_)
287 {
288 __get_db()->__iterator_copy(this, &__i);
289 }
290
Howard Hinnant99acc502010-09-21 17:32:39291 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58292 ~__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 Hinnantbc8d3f92010-05-11 19:42:16328
Howard Hinnant99acc502010-09-21 17:32:39329 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16330 __hash_iterator& operator++()
331 {
Howard Hinnant39213642013-07-23 22:01:58332#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 Hinnantbc8d3f92010-05-11 19:42:16336 __node_ = __node_->__next_;
337 return *this;
338 }
339
Howard Hinnant99acc502010-09-21 17:32:39340 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16341 __hash_iterator operator++(int)
342 {
343 __hash_iterator __t(*this);
344 ++(*this);
345 return __t;
346 }
347
Howard Hinnant99acc502010-09-21 17:32:39348 friend _LIBCPP_INLINE_VISIBILITY
349 bool operator==(const __hash_iterator& __x, const __hash_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58350 {
Howard Hinnant39213642013-07-23 22:01:58351 return __x.__node_ == __y.__node_;
352 }
Howard Hinnant99acc502010-09-21 17:32:39353 friend _LIBCPP_INLINE_VISIBILITY
354 bool operator!=(const __hash_iterator& __x, const __hash_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58355 {return !(__x == __y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16356
357private:
Howard Hinnant39213642013-07-23 22:01:58358#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 Hinnant99acc502010-09-21 17:32:39366 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24367 __hash_iterator(__node_pointer __node) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16368 : __node_(__node)
369 {}
Howard Hinnant39213642013-07-23 22:01:58370#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16371
372 template <class, class, class, class> friend class __hash_table;
Howard Hinnant0f678bd2013-08-12 18:38:34373 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 Hinnantbc8d3f92010-05-11 19:42:16377};
378
Eric Fiselier774c7c52016-02-10 20:46:23379template <class _NodePtr>
Howard Hinnant0f678bd2013-08-12 18:38:34380class _LIBCPP_TYPE_VIS_ONLY __hash_const_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16381{
Eric Fiselier774c7c52016-02-10 20:46:23382 static_assert(!is_const<typename pointer_traits<_NodePtr>::element_type>::value, "");
383 typedef __hash_node_types<_NodePtr> _NodeTypes;
384 typedef _NodePtr __node_pointer;
Eric Fiselier0493d022016-02-18 00:20:34385
Eric Fiselier774c7c52016-02-10 20:46:23386 __node_pointer __node_;
Howard Hinnantbc8d3f92010-05-11 19:42:16387
388public:
Eric Fiselier0493d022016-02-18 00:20:34389 typedef __hash_iterator<_NodePtr> __non_const_iterator;
390
Eric Fiselier774c7c52016-02-10 20:46:23391 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 Hinnantbc8d3f92010-05-11 19:42:16397
Howard Hinnant39213642013-07-23 22:01:58398 _LIBCPP_INLINE_VISIBILITY __hash_const_iterator() _NOEXCEPT
Marshall Clow193ef032013-08-07 21:30:44399#if _LIBCPP_STD_VER > 11
400 : __node_(nullptr)
401#endif
Howard Hinnant39213642013-07-23 22:01:58402 {
403#if _LIBCPP_DEBUG_LEVEL >= 2
404 __get_db()->__insert_i(this);
405#endif
406 }
Howard Hinnant99acc502010-09-21 17:32:39407 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24408 __hash_const_iterator(const __non_const_iterator& __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16409 : __node_(__x.__node_)
Howard Hinnant39213642013-07-23 22:01:58410 {
411#if _LIBCPP_DEBUG_LEVEL >= 2
412 __get_db()->__iterator_copy(this, &__x);
413#endif
414 }
415
416#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16417
Howard Hinnant99acc502010-09-21 17:32:39418 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58419 __hash_const_iterator(const __hash_const_iterator& __i)
420 : __node_(__i.__node_)
421 {
422 __get_db()->__iterator_copy(this, &__i);
423 }
424
Howard Hinnant99acc502010-09-21 17:32:39425 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58426 ~__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 Hinnantbc8d3f92010-05-11 19:42:16462
Howard Hinnant99acc502010-09-21 17:32:39463 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16464 __hash_const_iterator& operator++()
465 {
Howard Hinnant39213642013-07-23 22:01:58466#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 Hinnantbc8d3f92010-05-11 19:42:16470 __node_ = __node_->__next_;
471 return *this;
472 }
473
Howard Hinnant99acc502010-09-21 17:32:39474 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16475 __hash_const_iterator operator++(int)
476 {
477 __hash_const_iterator __t(*this);
478 ++(*this);
479 return __t;
480 }
481
Howard Hinnant99acc502010-09-21 17:32:39482 friend _LIBCPP_INLINE_VISIBILITY
483 bool operator==(const __hash_const_iterator& __x, const __hash_const_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58484 {
Howard Hinnant39213642013-07-23 22:01:58485 return __x.__node_ == __y.__node_;
486 }
Howard Hinnant99acc502010-09-21 17:32:39487 friend _LIBCPP_INLINE_VISIBILITY
488 bool operator!=(const __hash_const_iterator& __x, const __hash_const_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58489 {return !(__x == __y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16490
491private:
Howard Hinnant39213642013-07-23 22:01:58492#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 Hinnant99acc502010-09-21 17:32:39500 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24501 __hash_const_iterator(__node_pointer __node) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16502 : __node_(__node)
503 {}
Howard Hinnant39213642013-07-23 22:01:58504#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16505
506 template <class, class, class, class> friend class __hash_table;
Howard Hinnant0f678bd2013-08-12 18:38:34507 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 Hinnantbc8d3f92010-05-11 19:42:16510};
511
Howard Hinnantbc8d3f92010-05-11 19:42:16512template <class _NodePtr>
Howard Hinnant0f678bd2013-08-12 18:38:34513class _LIBCPP_TYPE_VIS_ONLY __hash_local_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16514{
Eric Fiselier774c7c52016-02-10 20:46:23515 typedef __hash_node_types<_NodePtr> _NodeTypes;
516 typedef _NodePtr __node_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16517
518 __node_pointer __node_;
519 size_t __bucket_;
520 size_t __bucket_count_;
521
Howard Hinnantbc8d3f92010-05-11 19:42:16522public:
523 typedef forward_iterator_tag iterator_category;
Eric Fiselier774c7c52016-02-10 20:46:23524 typedef typename _NodeTypes::__node_value_type value_type;
525 typedef typename _NodeTypes::difference_type difference_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16526 typedef value_type& reference;
Eric Fiselier774c7c52016-02-10 20:46:23527 typedef typename _NodeTypes::__node_value_type_pointer pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16528
Howard Hinnant39213642013-07-23 22:01:58529 _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 Hinnantbc8d3f92010-05-11 19:42:16537
Howard Hinnant99acc502010-09-21 17:32:39538 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58539 __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 Hinnant99acc502010-09-21 17:32:39547 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58548 ~__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 Hinnantbc8d3f92010-05-11 19:42:16586
Howard Hinnant99acc502010-09-21 17:32:39587 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16588 __hash_local_iterator& operator++()
589 {
Howard Hinnant39213642013-07-23 22:01:58590#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 Hinnantbc8d3f92010-05-11 19:42:16594 __node_ = __node_->__next_;
Howard Hinnant7a445152012-07-06 17:31:14595 if (__node_ != nullptr && __constrain_hash(__node_->__hash_, __bucket_count_) != __bucket_)
Howard Hinnantbc8d3f92010-05-11 19:42:16596 __node_ = nullptr;
597 return *this;
598 }
599
Howard Hinnant99acc502010-09-21 17:32:39600 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16601 __hash_local_iterator operator++(int)
602 {
603 __hash_local_iterator __t(*this);
604 ++(*this);
605 return __t;
606 }
607
Howard Hinnant99acc502010-09-21 17:32:39608 friend _LIBCPP_INLINE_VISIBILITY
609 bool operator==(const __hash_local_iterator& __x, const __hash_local_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58610 {
Howard Hinnant39213642013-07-23 22:01:58611 return __x.__node_ == __y.__node_;
612 }
Howard Hinnant99acc502010-09-21 17:32:39613 friend _LIBCPP_INLINE_VISIBILITY
614 bool operator!=(const __hash_local_iterator& __x, const __hash_local_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58615 {return !(__x == __y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16616
617private:
Howard Hinnant39213642013-07-23 22:01:58618#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 Hinnant99acc502010-09-21 17:32:39631 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16632 __hash_local_iterator(__node_pointer __node, size_t __bucket,
Howard Hinnant5f2f14c2011-06-04 18:54:24633 size_t __bucket_count) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16634 : __node_(__node),
635 __bucket_(__bucket),
636 __bucket_count_(__bucket_count)
637 {
638 if (__node_ != nullptr)
639 __node_ = __node_->__next_;
640 }
Howard Hinnant39213642013-07-23 22:01:58641#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16642 template <class, class, class, class> friend class __hash_table;
Howard Hinnant0f678bd2013-08-12 18:38:34643 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 Hinnantbc8d3f92010-05-11 19:42:16645};
646
647template <class _ConstNodePtr>
Howard Hinnant0f678bd2013-08-12 18:38:34648class _LIBCPP_TYPE_VIS_ONLY __hash_const_local_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16649{
Eric Fiselier774c7c52016-02-10 20:46:23650 typedef __hash_node_types<_ConstNodePtr> _NodeTypes;
651 typedef _ConstNodePtr __node_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16652
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 Fiselier5cf84e02015-12-30 21:52:00660 typedef typename __rebind_pointer<__node_pointer, __non_const_node>::type
661 __non_const_node_pointer;
Eric Fiselier0493d022016-02-18 00:20:34662public:
Howard Hinnantbc8d3f92010-05-11 19:42:16663 typedef __hash_local_iterator<__non_const_node_pointer>
664 __non_const_iterator;
Eric Fiselier0493d022016-02-18 00:20:34665
Eric Fiselier774c7c52016-02-10 20:46:23666 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 Fiselier5cf84e02015-12-30 21:52:00671
Howard Hinnantbc8d3f92010-05-11 19:42:16672
Howard Hinnant39213642013-07-23 22:01:58673 _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 Hinnant99acc502010-09-21 17:32:39680 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24681 __hash_const_local_iterator(const __non_const_iterator& __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16682 : __node_(__x.__node_),
683 __bucket_(__x.__bucket_),
684 __bucket_count_(__x.__bucket_count_)
Howard Hinnant39213642013-07-23 22:01:58685 {
686#if _LIBCPP_DEBUG_LEVEL >= 2
687 __get_db()->__iterator_copy(this, &__x);
688#endif
689 }
690
691#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16692
Howard Hinnant99acc502010-09-21 17:32:39693 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58694 __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 Hinnant99acc502010-09-21 17:32:39702 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58703 ~__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 Hinnantbc8d3f92010-05-11 19:42:16741
Howard Hinnant99acc502010-09-21 17:32:39742 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16743 __hash_const_local_iterator& operator++()
744 {
Howard Hinnant39213642013-07-23 22:01:58745#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 Hinnantbc8d3f92010-05-11 19:42:16749 __node_ = __node_->__next_;
Howard Hinnant7a445152012-07-06 17:31:14750 if (__node_ != nullptr && __constrain_hash(__node_->__hash_, __bucket_count_) != __bucket_)
Howard Hinnantbc8d3f92010-05-11 19:42:16751 __node_ = nullptr;
752 return *this;
753 }
754
Howard Hinnant99acc502010-09-21 17:32:39755 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16756 __hash_const_local_iterator operator++(int)
757 {
758 __hash_const_local_iterator __t(*this);
759 ++(*this);
760 return __t;
761 }
762
Howard Hinnant99acc502010-09-21 17:32:39763 friend _LIBCPP_INLINE_VISIBILITY
764 bool operator==(const __hash_const_local_iterator& __x, const __hash_const_local_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58765 {
Howard Hinnant39213642013-07-23 22:01:58766 return __x.__node_ == __y.__node_;
767 }
Howard Hinnant99acc502010-09-21 17:32:39768 friend _LIBCPP_INLINE_VISIBILITY
769 bool operator!=(const __hash_const_local_iterator& __x, const __hash_const_local_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58770 {return !(__x == __y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16771
772private:
Howard Hinnant39213642013-07-23 22:01:58773#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 Hinnant99acc502010-09-21 17:32:39786 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16787 __hash_const_local_iterator(__node_pointer __node, size_t __bucket,
Howard Hinnant5f2f14c2011-06-04 18:54:24788 size_t __bucket_count) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16789 : __node_(__node),
790 __bucket_(__bucket),
791 __bucket_count_(__bucket_count)
792 {
793 if (__node_ != nullptr)
794 __node_ = __node_->__next_;
795 }
Howard Hinnant39213642013-07-23 22:01:58796#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16797 template <class, class, class, class> friend class __hash_table;
Howard Hinnant0f678bd2013-08-12 18:38:34798 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_map_const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16799};
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 Hinnant99acc502010-09-21 17:32:39812 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16813 __bucket_list_deallocator()
Howard Hinnant5f2f14c2011-06-04 18:54:24814 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16815 : __data_(0) {}
Howard Hinnant99acc502010-09-21 17:32:39816
817 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16818 __bucket_list_deallocator(const allocator_type& __a, size_type __size)
Howard Hinnant5f2f14c2011-06-04 18:54:24819 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16820 : __data_(__size, __a) {}
821
Howard Hinnant73d21a42010-09-04 23:28:19822#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16823
Howard Hinnant99acc502010-09-21 17:32:39824 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16825 __bucket_list_deallocator(__bucket_list_deallocator&& __x)
Howard Hinnant5f2f14c2011-06-04 18:54:24826 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnant0949eed2011-06-30 21:18:19827 : __data_(_VSTD::move(__x.__data_))
Howard Hinnantbc8d3f92010-05-11 19:42:16828 {
829 __x.size() = 0;
830 }
831
Howard Hinnant73d21a42010-09-04 23:28:19832#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16833
Howard Hinnant5f2f14c2011-06-04 18:54:24834 _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 Hinnantbc8d3f92010-05-11 19:42:16838
Howard Hinnant99acc502010-09-21 17:32:39839 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24840 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 Hinnantbc8d3f92010-05-11 19:42:16846 {
847 __alloc_traits::deallocate(__alloc(), __p, size());
848 }
849};
850
Howard Hinnant2b1b2d42011-06-14 19:58:17851template <class _Alloc> class __hash_map_node_destructor;
Howard Hinnantbc8d3f92010-05-11 19:42:16852
853template <class _Alloc>
854class __hash_node_destructor
855{
856 typedef _Alloc allocator_type;
857 typedef allocator_traits<allocator_type> __alloc_traits;
Eric Fiselier774c7c52016-02-10 20:46:23858
Howard Hinnantbc8d3f92010-05-11 19:42:16859public:
860 typedef typename __alloc_traits::pointer pointer;
861private:
Eric Fiselier2960ae22016-02-11 11:59:44862 typedef __hash_node_types<pointer> _NodeTypes;
Howard Hinnantbc8d3f92010-05-11 19:42:16863
864 allocator_type& __na_;
865
866 __hash_node_destructor& operator=(const __hash_node_destructor&);
867
868public:
869 bool __value_constructed;
870
Howard Hinnant99acc502010-09-21 17:32:39871 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant199d0ae2011-07-31 17:04:30872 explicit __hash_node_destructor(allocator_type& __na,
873 bool __constructed = false) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16874 : __na_(__na),
Howard Hinnant199d0ae2011-07-31 17:04:30875 __value_constructed(__constructed)
Howard Hinnantbc8d3f92010-05-11 19:42:16876 {}
877
Howard Hinnant99acc502010-09-21 17:32:39878 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24879 void operator()(pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16880 {
881 if (__value_constructed)
Eric Fiselier2960ae22016-02-11 11:59:44882 __alloc_traits::destroy(__na_, _NodeTypes::__get_ptr(__p->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16883 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 Fiselier774c7c52016-02-10 20:46:23901 typedef typename
902 __make_hash_node_types<value_type, typename __alloc_traits::void_pointer>::type
903 _NodeTypes;
Howard Hinnantbc8d3f92010-05-11 19:42:16904public:
Eric Fiselier2960ae22016-02-11 11:59:44905
906 typedef typename _NodeTypes::__node_value_type __node_value_type;
907 typedef typename _NodeTypes::__container_value_type __container_value_type;
Duncan P. N. Exon Smith798ec842016-03-17 20:45:20908 typedef typename _NodeTypes::key_type key_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16909 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 Fiselier774c7c52016-02-10 20:46:23913#ifndef _LIBCPP_ABI_FIX_UNORDERED_CONTAINER_SIZE_TYPE
Howard Hinnantbc8d3f92010-05-11 19:42:16914 typedef typename __alloc_traits::size_type size_type;
Eric Fiselier774c7c52016-02-10 20:46:23915#else
916 typedef typename _NodeTypes::size_type size_type;
917#endif
918 typedef typename _NodeTypes::difference_type difference_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16919public:
920 // Create __node
Eric Fiselier774c7c52016-02-10 20:46:23921
922 typedef typename _NodeTypes::__node_type __node;
Marshall Clow66302c62015-04-07 05:21:38923 typedef typename __rebind_alloc_helper<__alloc_traits, __node>::type __node_allocator;
Howard Hinnantbc8d3f92010-05-11 19:42:16924 typedef allocator_traits<__node_allocator> __node_traits;
Eric Fiselier9e9f42e2016-02-11 15:22:37925 typedef typename _NodeTypes::__void_pointer __void_pointer;
Eric Fiselier774c7c52016-02-10 20:46:23926 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 Hinnantbc8d3f92010-05-11 19:42:16942
943private:
944
Marshall Clow66302c62015-04-07 05:21:38945 typedef typename __rebind_alloc_helper<__node_traits, __node_pointer>::type __pointer_allocator;
Howard Hinnantbc8d3f92010-05-11 19:42:16946 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 Fiselier774c7c52016-02-10 20:46:23952 __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 Hinnantbc8d3f92010-05-11 19:42:16956 // --- Member data end ---
957
Howard Hinnant5f2f14c2011-06-04 18:54:24958 _LIBCPP_INLINE_VISIBILITY
959 size_type& size() _NOEXCEPT {return __p2_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16960public:
Howard Hinnant5f2f14c2011-06-04 18:54:24961 _LIBCPP_INLINE_VISIBILITY
962 size_type size() const _NOEXCEPT {return __p2_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16963
Howard Hinnant5f2f14c2011-06-04 18:54:24964 _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 Hinnantbc8d3f92010-05-11 19:42:16968
Howard Hinnant5f2f14c2011-06-04 18:54:24969 _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 Hinnantbc8d3f92010-05-11 19:42:16973
Howard Hinnant5f2f14c2011-06-04 18:54:24974 _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 Hinnantbc8d3f92010-05-11 19:42:16978
Howard Hinnant5f2f14c2011-06-04 18:54:24979 _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 Hinnantbc8d3f92010-05-11 19:42:16984
985public:
986 typedef __hash_iterator<__node_pointer> iterator;
Howard Hinnant7a6b7ce2013-06-22 15:21:29987 typedef __hash_const_iterator<__node_pointer> const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16988 typedef __hash_local_iterator<__node_pointer> local_iterator;
Howard Hinnant7a6b7ce2013-06-22 15:21:29989 typedef __hash_const_local_iterator<__node_pointer> const_local_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16990
Evgeniy Stepanova3b25f82015-11-07 01:22:13991 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24992 __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 Stepanova3b25f82015-11-07 01:22:13999 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161000 __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 Fiselier2960ae22016-02-11 11:59:441006#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant5f2f14c2011-06-04 18:54:241007 __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 Hinnantbc8d3f92010-05-11 19:42:161014 __hash_table(__hash_table&& __u, const allocator_type& __a);
Eric Fiselier2960ae22016-02-11 11:59:441015#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:161016 ~__hash_table();
1017
1018 __hash_table& operator=(const __hash_table& __u);
Eric Fiselier2960ae22016-02-11 11:59:441019#ifndef _LIBCPP_CXX03_LANG
Evgeniy Stepanova3b25f82015-11-07 01:22:131020 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:241021 __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 Hinnantbc8d3f92010-05-11 19:42:161027#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 Hinnant99acc502010-09-21 17:32:391033 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:241034 size_type max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161035 {
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 Fiselier2960ae22016-02-11 11:59:441045#ifndef _LIBCPP_CXX03_LANG
1046 template <class _Key, class ..._Args>
Duncan P. N. Exon Smith798ec842016-03-17 20:45:201047 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2960ae22016-02-11 11:59:441048 pair<iterator, bool> __emplace_unique_key_args(_Key const& __k, _Args&&... __args);
Howard Hinnantbc8d3f92010-05-11 19:42:161049
Eric Fiselier2960ae22016-02-11 11:59:441050 template <class... _Args>
Duncan P. N. Exon Smith798ec842016-03-17 20:45:201051 _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 Fiselierdc414cd2016-04-16 00:23:121060
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 Fiselier2960ae22016-02-11 11:59:441071 template <class... _Args>
Duncan P. N. Exon Smith798ec842016-03-17 20:45:201072 _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 Fiselier2960ae22016-02-11 11:59:441098 iterator __emplace_multi(_Args&&... __args);
1099 template <class... _Args>
Duncan P. N. Exon Smith798ec842016-03-17 20:45:201100 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2960ae22016-02-11 11:59:441101 iterator __emplace_hint_multi(const_iterator __p, _Args&&... __args);
1102
1103
Eric Fiselierfdae69a2015-06-13 07:18:321104 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2960ae22016-02-11 11:59:441105 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 Fiselierfdae69a2015-06-13 07:18:321113 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2960ae22016-02-11 11:59:441114 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 Fiselierfdae69a2015-06-13 07:18:321136#endif
1137
Eric Fiselier2960ae22016-02-11 11:59:441138 _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 Hinnantbc8d3f92010-05-11 19:42:161142
Howard Hinnant5f2f14c2011-06-04 18:54:241143 void clear() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:161144 void rehash(size_type __n);
Howard Hinnant99acc502010-09-21 17:32:391145 _LIBCPP_INLINE_VISIBILITY void reserve(size_type __n)
Howard Hinnantbc8d3f92010-05-11 19:42:161146 {rehash(static_cast<size_type>(ceil(__n / max_load_factor())));}
Howard Hinnant99acc502010-09-21 17:32:391147
1148 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:241149 size_type bucket_count() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161150 {
1151 return __bucket_list_.get_deleter().size();
1152 }
1153
Evgeniy Stepanova3b25f82015-11-07 01:22:131154 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:241155 iterator begin() _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:131156 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:241157 iterator end() _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:131158 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:241159 const_iterator begin() const _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:131160 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:241161 const_iterator end() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:161162
1163 template <class _Key>
Howard Hinnant99acc502010-09-21 17:32:391164 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161165 size_type bucket(const _Key& __k) const
Howard Hinnant0bb0a7c2013-07-29 19:05:471166 {
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 Hinnantbc8d3f92010-05-11 19:42:161171
1172 template <class _Key>
1173 iterator find(const _Key& __x);
1174 template <class _Key>
1175 const_iterator find(const _Key& __x) const;
1176
Howard Hinnant99968442011-11-29 18:15:501177 typedef __hash_node_destructor<__node_allocator> _Dp;
1178 typedef unique_ptr<__node, _Dp> __node_holder;
Howard Hinnantbc8d3f92010-05-11 19:42:161179
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 Hinnant5f2f14c2011-06-04 18:54:241186 __node_holder remove(const_iterator __p) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:161187
1188 template <class _Key>
Evgeniy Stepanova3b25f82015-11-07 01:22:131189 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161190 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 Hinnant5f2f14c2011-06-04 18:54:241208 void swap(__hash_table& __u)
Eric Fiselier692177d2015-07-18 20:40:461209#if _LIBCPP_STD_VER <= 11
Howard Hinnant5f2f14c2011-06-04 18:54:241210 _NOEXCEPT_(
Marshall Clow7d914d12015-07-13 20:04:561211 __is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value
Marshall Clow7d914d12015-07-13 20:04:561212 && (!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 Clow7d914d12015-07-13 20:04:561216 );
Eric Fiselier692177d2015-07-18 20:40:461217#else
1218 _NOEXCEPT_(__is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value);
1219#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161220
Howard Hinnant99acc502010-09-21 17:32:391221 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:241222 size_type max_bucket_count() const _NOEXCEPT
Howard Hinnant7a6b7ce2013-06-22 15:21:291223 {return __pointer_alloc_traits::max_size(__bucket_list_.get_deleter().__alloc());}
Howard Hinnantbc8d3f92010-05-11 19:42:161224 size_type bucket_size(size_type __n) const;
Howard Hinnant5f2f14c2011-06-04 18:54:241225 _LIBCPP_INLINE_VISIBILITY float load_factor() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161226 {
1227 size_type __bc = bucket_count();
1228 return __bc != 0 ? (float)size() / __bc : 0.f;
1229 }
Howard Hinnant5f2f14c2011-06-04 18:54:241230 _LIBCPP_INLINE_VISIBILITY void max_load_factor(float __mlf) _NOEXCEPT
Howard Hinnant0bb0a7c2013-07-29 19:05:471231 {
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 Hinnantbc8d3f92010-05-11 19:42:161236
Howard Hinnant39213642013-07-23 22:01:581237 _LIBCPP_INLINE_VISIBILITY
1238 local_iterator
1239 begin(size_type __n)
1240 {
Howard Hinnant0bb0a7c2013-07-29 19:05:471241 _LIBCPP_ASSERT(__n < bucket_count(),
1242 "unordered container::begin(n) called with n >= bucket_count()");
Howard Hinnant39213642013-07-23 22:01:581243#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 Hinnant0bb0a7c2013-07-29 19:05:471254 _LIBCPP_ASSERT(__n < bucket_count(),
1255 "unordered container::end(n) called with n >= bucket_count()");
Howard Hinnant39213642013-07-23 22:01:581256#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 Hinnant0bb0a7c2013-07-29 19:05:471267 _LIBCPP_ASSERT(__n < bucket_count(),
1268 "unordered container::cbegin(n) called with n >= bucket_count()");
Howard Hinnant39213642013-07-23 22:01:581269#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 Hinnant0bb0a7c2013-07-29 19:05:471280 _LIBCPP_ASSERT(__n < bucket_count(),
1281 "unordered container::cend(n) called with n >= bucket_count()");
Howard Hinnant39213642013-07-23 22:01:581282#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 Hinnantbc8d3f92010-05-11 19:42:161298private:
1299 void __rehash(size_type __n);
1300
Eric Fiselier2960ae22016-02-11 11:59:441301#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:161302 template <class ..._Args>
Eric Fiselier2960ae22016-02-11 11:59:441303 __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 Hinnantbc8d3f92010-05-11 19:42:161310#endif
Eric Fiselier2960ae22016-02-11 11:59:441311
Howard Hinnantbc8d3f92010-05-11 19:42:161312
Howard Hinnant99acc502010-09-21 17:32:391313 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161314 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 Hinnant99acc502010-09-21 17:32:391318 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:041319 void __copy_assign_alloc(const __hash_table&, false_type) {}
Howard Hinnantbc8d3f92010-05-11 19:42:161320
Eric Fiselier2960ae22016-02-11 11:59:441321#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:161322 void __move_assign(__hash_table& __u, false_type);
Howard Hinnant5f2f14c2011-06-04 18:54:241323 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 Hinnantbc8d3f92010-05-11 19:42:161334 {__move_assign_alloc(__u, integral_constant<bool,
1335 __node_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnant99acc502010-09-21 17:32:391336 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:161337 void __move_assign_alloc(__hash_table& __u, true_type)
Howard Hinnant5f2f14c2011-06-04 18:54:241338 _NOEXCEPT_(
1339 is_nothrow_move_assignable<__pointer_allocator>::value &&
1340 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:161341 {
1342 __bucket_list_.get_deleter().__alloc() =
Howard Hinnant0949eed2011-06-30 21:18:191343 _VSTD::move(__u.__bucket_list_.get_deleter().__alloc());
1344 __node_alloc() = _VSTD::move(__u.__node_alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:161345 }
Howard Hinnant99acc502010-09-21 17:32:391346 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:241347 void __move_assign_alloc(__hash_table&, false_type) _NOEXCEPT {}
Eric Fiselier2960ae22016-02-11 11:59:441348#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:161349
Howard Hinnant5f2f14c2011-06-04 18:54:241350 void __deallocate(__node_pointer __np) _NOEXCEPT;
1351 __node_pointer __detach() _NOEXCEPT;
Howard Hinnant7a6b7ce2013-06-22 15:21:291352
Howard Hinnant0f678bd2013-08-12 18:38:341353 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 Hinnantbc8d3f92010-05-11 19:42:161355};
1356
1357template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:131358inline
Howard Hinnantbc8d3f92010-05-11 19:42:161359__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table()
Howard Hinnant5f2f14c2011-06-04 18:54:241360 _NOEXCEPT_(
1361 is_nothrow_default_constructible<__bucket_list>::value &&
1362 is_nothrow_default_constructible<__first_node>::value &&
Eric Fiselierc8f54c22015-12-16 00:53:041363 is_nothrow_default_constructible<__node_allocator>::value &&
Howard Hinnant5f2f14c2011-06-04 18:54:241364 is_nothrow_default_constructible<hasher>::value &&
1365 is_nothrow_default_constructible<key_equal>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:161366 : __p2_(0),
1367 __p3_(1.0f)
1368{
1369}
1370
1371template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:131372inline
Howard Hinnantbc8d3f92010-05-11 19:42:161373__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 Fiselier2960ae22016-02-11 11:59:441425#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:161426
1427template <class _Tp, class _Hash, class _Equal, class _Alloc>
1428__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(__hash_table&& __u)
Howard Hinnant5f2f14c2011-06-04 18:54:241429 _NOEXCEPT_(
1430 is_nothrow_move_constructible<__bucket_list>::value &&
1431 is_nothrow_move_constructible<__first_node>::value &&
Eric Fiselierc8f54c22015-12-16 00:53:041432 is_nothrow_move_constructible<__node_allocator>::value &&
Howard Hinnant5f2f14c2011-06-04 18:54:241433 is_nothrow_move_constructible<hasher>::value &&
1434 is_nothrow_move_constructible<key_equal>::value)
Howard Hinnant0949eed2011-06-30 21:18:191435 : __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 Hinnantbc8d3f92010-05-11 19:42:161439{
1440 if (size() > 0)
1441 {
Howard Hinnant7a445152012-07-06 17:31:141442 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash_, bucket_count())] =
Howard Hinnant7a6b7ce2013-06-22 15:21:291443 static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
Howard Hinnantbc8d3f92010-05-11 19:42:161444 __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 Hinnant0949eed2011-06-30 21:18:191454 __p2_(0, _VSTD::move(__u.hash_function())),
1455 __p3_(_VSTD::move(__u.__p3_))
Howard Hinnantbc8d3f92010-05-11 19:42:161456{
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 Hinnant7a445152012-07-06 17:31:141466 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash_, bucket_count())] =
Howard Hinnant7a6b7ce2013-06-22 15:21:291467 static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
Howard Hinnantbc8d3f92010-05-11 19:42:161468 size() = __u.size();
1469 __u.size() = 0;
1470 }
1471 }
1472}
1473
Eric Fiselier2960ae22016-02-11 11:59:441474#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:161475
1476template <class _Tp, class _Hash, class _Equal, class _Alloc>
1477__hash_table<_Tp, _Hash, _Equal, _Alloc>::~__hash_table()
1478{
Marshall Clow1a933122016-06-30 22:05:451479 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 Hinnantbc8d3f92010-05-11 19:42:161483 __deallocate(__p1_.first().__next_);
Howard Hinnant39213642013-07-23 22:01:581484#if _LIBCPP_DEBUG_LEVEL >= 2
1485 __get_db()->__erase_c(this);
1486#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161487}
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 Hinnant5f2f14c2011-06-04 18:54:241522 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161523{
1524 __node_allocator& __na = __node_alloc();
1525 while (__np != nullptr)
1526 {
1527 __node_pointer __next = __np->__next_;
Howard Hinnant39213642013-07-23 22:01:581528#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 Fiselier2960ae22016-02-11 11:59:441543 __node_traits::destroy(__na, _NodeTypes::__get_ptr(__np->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:161544 __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 Hinnant5f2f14c2011-06-04 18:54:241551__hash_table<_Tp, _Hash, _Equal, _Alloc>::__detach() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161552{
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 Fiselier2960ae22016-02-11 11:59:441562#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:161563
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 Hinnant5f2f14c2011-06-04 18:54:241568 _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 Hinnantbc8d3f92010-05-11 19:42:161572{
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 Hinnant0949eed2011-06-30 21:18:191579 hash_function() = _VSTD::move(__u.hash_function());
Howard Hinnantbc8d3f92010-05-11 19:42:161580 max_load_factor() = __u.max_load_factor();
Howard Hinnant0949eed2011-06-30 21:18:191581 key_eq() = _VSTD::move(__u.key_eq());
Howard Hinnantbc8d3f92010-05-11 19:42:161582 __p1_.first().__next_ = __u.__p1_.first().__next_;
1583 if (size() > 0)
1584 {
Howard Hinnant7a445152012-07-06 17:31:141585 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash_, bucket_count())] =
Howard Hinnant7a6b7ce2013-06-22 15:21:291586 static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
Howard Hinnantbc8d3f92010-05-11 19:42:161587 __u.__p1_.first().__next_ = nullptr;
1588 __u.size() = 0;
1589 }
Howard Hinnant39213642013-07-23 22:01:581590#if _LIBCPP_DEBUG_LEVEL >= 2
1591 __get_db()->swap(this, &__u);
1592#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161593}
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 Hinnant0949eed2011-06-30 21:18:191604 hash_function() = _VSTD::move(__u.hash_function());
1605 key_eq() = _VSTD::move(__u.key_eq());
Howard Hinnantbc8d3f92010-05-11 19:42:161606 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 Hinnant324bb032010-08-22 00:02:431613#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:161614 const_iterator __i = __u.begin();
1615 while (__cache != nullptr && __u.size() != 0)
1616 {
Howard Hinnant0949eed2011-06-30 21:18:191617 __cache->__value_ = _VSTD::move(__u.remove(__i++)->__value_);
Howard Hinnantbc8d3f92010-05-11 19:42:161618 __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 Hinnant324bb032010-08-22 00:02:431629#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:161630 __deallocate(__cache);
1631 }
1632 const_iterator __i = __u.begin();
1633 while (__u.size() != 0)
1634 {
Eric Fiselier2960ae22016-02-11 11:59:441635 __node_holder __h = __construct_node(_NodeTypes::__move(__u.remove(__i++)->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:161636 __node_insert_multi(__h.get());
1637 __h.release();
1638 }
1639 }
1640}
1641
1642template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:131643inline
Howard Hinnantbc8d3f92010-05-11 19:42:161644__hash_table<_Tp, _Hash, _Equal, _Alloc>&
1645__hash_table<_Tp, _Hash, _Equal, _Alloc>::operator=(__hash_table&& __u)
Howard Hinnant5f2f14c2011-06-04 18:54:241646 _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 Hinnantbc8d3f92010-05-11 19:42:161651{
1652 __move_assign(__u, integral_constant<bool,
1653 __node_traits::propagate_on_container_move_assignment::value>());
1654 return *this;
1655}
1656
Eric Fiselier2960ae22016-02-11 11:59:441657#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:161658
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 Fiselier2960ae22016-02-11 11:59:441665 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 Hinnantbc8d3f92010-05-11 19:42:161670 if (bucket_count() != 0)
1671 {
1672 __node_pointer __cache = __detach();
1673#ifndef _LIBCPP_NO_EXCEPTIONS
1674 try
1675 {
Howard Hinnant324bb032010-08-22 00:02:431676#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:161677 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 Hinnant324bb032010-08-22 00:02:431691#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:161692 __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 Fiselier2960ae22016-02-11 11:59:441704 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 Hinnantbc8d3f92010-05-11 19:42:161710 if (bucket_count() != 0)
1711 {
1712 __node_pointer __cache = __detach();
1713#ifndef _LIBCPP_NO_EXCEPTIONS
1714 try
1715 {
Howard Hinnant324bb032010-08-22 00:02:431716#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:161717 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 Hinnant324bb032010-08-22 00:02:431731#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:161732 __deallocate(__cache);
1733 }
1734 for (; __first != __last; ++__first)
Eric Fiselier2960ae22016-02-11 11:59:441735 __insert_multi(_NodeTypes::__get_value(*__first));
Howard Hinnantbc8d3f92010-05-11 19:42:161736}
1737
1738template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:131739inline
Howard Hinnantbc8d3f92010-05-11 19:42:161740typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
Howard Hinnant5f2f14c2011-06-04 18:54:241741__hash_table<_Tp, _Hash, _Equal, _Alloc>::begin() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161742{
Howard Hinnant39213642013-07-23 22:01:581743#if _LIBCPP_DEBUG_LEVEL >= 2
1744 return iterator(__p1_.first().__next_, this);
1745#else
Howard Hinnantbc8d3f92010-05-11 19:42:161746 return iterator(__p1_.first().__next_);
Howard Hinnant39213642013-07-23 22:01:581747#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161748}
1749
1750template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:131751inline
Howard Hinnantbc8d3f92010-05-11 19:42:161752typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
Howard Hinnant5f2f14c2011-06-04 18:54:241753__hash_table<_Tp, _Hash, _Equal, _Alloc>::end() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161754{
Howard Hinnant39213642013-07-23 22:01:581755#if _LIBCPP_DEBUG_LEVEL >= 2
1756 return iterator(nullptr, this);
1757#else
Howard Hinnantbc8d3f92010-05-11 19:42:161758 return iterator(nullptr);
Howard Hinnant39213642013-07-23 22:01:581759#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161760}
1761
1762template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:131763inline
Howard Hinnantbc8d3f92010-05-11 19:42:161764typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
Howard Hinnant5f2f14c2011-06-04 18:54:241765__hash_table<_Tp, _Hash, _Equal, _Alloc>::begin() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161766{
Howard Hinnant39213642013-07-23 22:01:581767#if _LIBCPP_DEBUG_LEVEL >= 2
1768 return const_iterator(__p1_.first().__next_, this);
1769#else
Howard Hinnantbc8d3f92010-05-11 19:42:161770 return const_iterator(__p1_.first().__next_);
Howard Hinnant39213642013-07-23 22:01:581771#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161772}
1773
1774template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:131775inline
Howard Hinnantbc8d3f92010-05-11 19:42:161776typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
Howard Hinnant5f2f14c2011-06-04 18:54:241777__hash_table<_Tp, _Hash, _Equal, _Alloc>::end() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161778{
Howard Hinnant39213642013-07-23 22:01:581779#if _LIBCPP_DEBUG_LEVEL >= 2
1780 return const_iterator(nullptr, this);
1781#else
Howard Hinnantbc8d3f92010-05-11 19:42:161782 return const_iterator(nullptr);
Howard Hinnant39213642013-07-23 22:01:581783#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161784}
1785
1786template <class _Tp, class _Hash, class _Equal, class _Alloc>
1787void
Howard Hinnant5f2f14c2011-06-04 18:54:241788__hash_table<_Tp, _Hash, _Equal, _Alloc>::clear() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161789{
1790 if (size() > 0)
1791 {
1792 __deallocate(__p1_.first().__next_);
1793 __p1_.first().__next_ = nullptr;
1794 size_type __bc = bucket_count();
Howard Hinnant9f66bff2011-07-05 14:14:171795 for (size_type __i = 0; __i < __bc; ++__i)
Howard Hinnantbc8d3f92010-05-11 19:42:161796 __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 Hinnant7a445152012-07-06 17:31:141812 __chash = __constrain_hash(__nd->__hash_, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:161813 __ndptr = __bucket_list_[__chash];
1814 if (__ndptr != nullptr)
1815 {
1816 for (__ndptr = __ndptr->__next_; __ndptr != nullptr &&
Howard Hinnant7a445152012-07-06 17:31:141817 __constrain_hash(__ndptr->__hash_, __bc) == __chash;
Howard Hinnantbc8d3f92010-05-11 19:42:161818 __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 Fiselier57947ca2015-02-02 21:31:481828 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnantbc8d3f92010-05-11 19:42:161829 size_type(ceil(float(size() + 1) / max_load_factor()))));
1830 __bc = bucket_count();
Howard Hinnant7a445152012-07-06 17:31:141831 __chash = __constrain_hash(__nd->__hash_, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:161832 }
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 Hinnant7a6b7ce2013-06-22 15:21:291837 __pn = static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
Howard Hinnantbc8d3f92010-05-11 19:42:161838 __nd->__next_ = __pn->__next_;
1839 __pn->__next_ = __nd;
1840 // fix up __bucket_list_
1841 __bucket_list_[__chash] = __pn;
1842 if (__nd->__next_ != nullptr)
Howard Hinnant7a445152012-07-06 17:31:141843 __bucket_list_[__constrain_hash(__nd->__next_->__hash_, __bc)] = __nd;
Howard Hinnantbc8d3f92010-05-11 19:42:161844 }
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 Hinnant39213642013-07-23 22:01:581856#if _LIBCPP_DEBUG_LEVEL >= 2
1857 return pair<iterator, bool>(iterator(__ndptr, this), __inserted);
1858#else
Howard Hinnantbc8d3f92010-05-11 19:42:161859 return pair<iterator, bool>(iterator(__ndptr), __inserted);
Howard Hinnant39213642013-07-23 22:01:581860#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161861}
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 Fiselier57947ca2015-02-02 21:31:481871 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnantbc8d3f92010-05-11 19:42:161872 size_type(ceil(float(size() + 1) / max_load_factor()))));
1873 __bc = bucket_count();
1874 }
Howard Hinnant7a445152012-07-06 17:31:141875 size_t __chash = __constrain_hash(__cp->__hash_, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:161876 __node_pointer __pn = __bucket_list_[__chash];
1877 if (__pn == nullptr)
1878 {
Howard Hinnant7a6b7ce2013-06-22 15:21:291879 __pn = static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
Howard Hinnantbc8d3f92010-05-11 19:42:161880 __cp->__next_ = __pn->__next_;
1881 __pn->__next_ = __cp;
1882 // fix up __bucket_list_
1883 __bucket_list_[__chash] = __pn;
1884 if (__cp->__next_ != nullptr)
Howard Hinnant7a445152012-07-06 17:31:141885 __bucket_list_[__constrain_hash(__cp->__next_->__hash_, __bc)] = __cp;
Howard Hinnantbc8d3f92010-05-11 19:42:161886 }
1887 else
1888 {
1889 for (bool __found = false; __pn->__next_ != nullptr &&
Howard Hinnant7a445152012-07-06 17:31:141890 __constrain_hash(__pn->__next_->__hash_, __bc) == __chash;
Howard Hinnantbc8d3f92010-05-11 19:42:161891 __pn = __pn->__next_)
Howard Hinnant324bb032010-08-22 00:02:431892 {
Howard Hinnantbc8d3f92010-05-11 19:42:161893 // __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 Hinnant7a445152012-07-06 17:31:141911 size_t __nhash = __constrain_hash(__cp->__next_->__hash_, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:161912 if (__nhash != __chash)
1913 __bucket_list_[__nhash] = __cp;
1914 }
1915 }
1916 ++size();
Howard Hinnant39213642013-07-23 22:01:581917#if _LIBCPP_DEBUG_LEVEL >= 2
1918 return iterator(__cp, this);
1919#else
Howard Hinnantbc8d3f92010-05-11 19:42:161920 return iterator(__cp);
Howard Hinnant39213642013-07-23 22:01:581921#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161922}
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 Hinnant824c1992013-08-02 17:50:491929#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 Hinnantbc8d3f92010-05-11 19:42:161934 if (__p != end() && key_eq()(*__p, __cp->__value_))
1935 {
Howard Hinnant7a6b7ce2013-06-22 15:21:291936 __node_pointer __np = __p.__node_;
Howard Hinnantbc8d3f92010-05-11 19:42:161937 __cp->__hash_ = __np->__hash_;
1938 size_type __bc = bucket_count();
1939 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1940 {
Eric Fiselier57947ca2015-02-02 21:31:481941 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnantbc8d3f92010-05-11 19:42:161942 size_type(ceil(float(size() + 1) / max_load_factor()))));
1943 __bc = bucket_count();
1944 }
Howard Hinnant7a445152012-07-06 17:31:141945 size_t __chash = __constrain_hash(__cp->__hash_, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:161946 __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 Hinnant39213642013-07-23 22:01:581952#if _LIBCPP_DEBUG_LEVEL >= 2
1953 return iterator(__cp, this);
1954#else
Howard Hinnantbc8d3f92010-05-11 19:42:161955 return iterator(__cp);
Howard Hinnant39213642013-07-23 22:01:581956#endif
Howard Hinnantbc8d3f92010-05-11 19:42:161957 }
1958 return __node_insert_multi(__cp);
1959}
1960
Eric Fiselierfdae69a2015-06-13 07:18:321961
1962
Eric Fiselier2960ae22016-02-11 11:59:441963#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierfdae69a2015-06-13 07:18:321964template <class _Tp, class _Hash, class _Equal, class _Alloc>
Eric Fiselier2960ae22016-02-11 11:59:441965template <class _Key, class ..._Args>
Eric Fiselierfdae69a2015-06-13 07:18:321966_LIBCPP_INLINE_VISIBILITY
1967pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
Eric Fiselier2960ae22016-02-11 11:59:441968__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_key_args(_Key const& __k, _Args&&... __args)
Eric Fiselierfdae69a2015-06-13 07:18:321969#else
1970template <class _Tp, class _Hash, class _Equal, class _Alloc>
Eric Fiselier2960ae22016-02-11 11:59:441971template <class _Key, class _Args>
Eric Fiselierfdae69a2015-06-13 07:18:321972_LIBCPP_INLINE_VISIBILITY
1973pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
Eric Fiselier2960ae22016-02-11 11:59:441974__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_key_args(_Key const& __k, _Args& __args)
Eric Fiselierfdae69a2015-06-13 07:18:321975#endif
1976{
Eric Fiselier2960ae22016-02-11 11:59:441977
1978 size_t __hash = hash_function()(__k);
Howard Hinnantbc8d3f92010-05-11 19:42:161979 size_type __bc = bucket_count();
1980 bool __inserted = false;
1981 __node_pointer __nd;
1982 size_t __chash;
1983 if (__bc != 0)
1984 {
Howard Hinnant7a445152012-07-06 17:31:141985 __chash = __constrain_hash(__hash, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:161986 __nd = __bucket_list_[__chash];
1987 if (__nd != nullptr)
1988 {
1989 for (__nd = __nd->__next_; __nd != nullptr &&
Howard Hinnant7a445152012-07-06 17:31:141990 __constrain_hash(__nd->__hash_, __bc) == __chash;
Howard Hinnantbc8d3f92010-05-11 19:42:161991 __nd = __nd->__next_)
1992 {
Eric Fiselier2960ae22016-02-11 11:59:441993 if (key_eq()(__nd->__value_, __k))
Howard Hinnantbc8d3f92010-05-11 19:42:161994 goto __done;
1995 }
1996 }
1997 }
1998 {
Eric Fiselier2960ae22016-02-11 11:59:441999#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 Hinnantbc8d3f92010-05-11 19:42:162004 if (size()+1 > __bc * max_load_factor() || __bc == 0)
2005 {
Eric Fiselier57947ca2015-02-02 21:31:482006 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnantbc8d3f92010-05-11 19:42:162007 size_type(ceil(float(size() + 1) / max_load_factor()))));
2008 __bc = bucket_count();
Howard Hinnant7a445152012-07-06 17:31:142009 __chash = __constrain_hash(__hash, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:162010 }
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 Fiselier9e9f42e2016-02-11 15:22:372015 __pn = static_cast<__node_pointer>(static_cast<__void_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first())));
Howard Hinnantbc8d3f92010-05-11 19:42:162016 __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 Hinnant7a445152012-07-06 17:31:142021 __bucket_list_[__constrain_hash(__h->__next_->__hash_, __bc)] = __h.get();
Howard Hinnantbc8d3f92010-05-11 19:42:162022 }
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 Hinnant39213642013-07-23 22:01:582034#if _LIBCPP_DEBUG_LEVEL >= 2
2035 return pair<iterator, bool>(iterator(__nd, this), __inserted);
2036#else
Howard Hinnantbc8d3f92010-05-11 19:42:162037 return pair<iterator, bool>(iterator(__nd), __inserted);
Howard Hinnant39213642013-07-23 22:01:582038#endif
Howard Hinnantbc8d3f92010-05-11 19:42:162039}
2040
Eric Fiselier2960ae22016-02-11 11:59:442041#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:162042
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 Smith798ec842016-03-17 20:45:202046__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_impl(_Args&&... __args)
Howard Hinnantbc8d3f92010-05-11 19:42:162047{
Howard Hinnant0949eed2011-06-30 21:18:192048 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:162049 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 Hinnant0949eed2011-06-30 21:18:192060 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:162061 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 Hinnant0bb0a7c2013-07-29 19:05:472072#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 Hinnant0949eed2011-06-30 21:18:192077 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:162078 iterator __r = __node_insert_multi(__p, __h.get());
2079 __h.release();
2080 return __r;
2081}
2082
Eric Fiselier2960ae22016-02-11 11:59:442083#else // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:162084
2085template <class _Tp, class _Hash, class _Equal, class _Alloc>
2086typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
Eric Fiselier2960ae22016-02-11 11:59:442087__hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const __container_value_type& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:162088{
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 Fiselier2960ae22016-02-11 11:59:442098 const __container_value_type& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:162099{
Howard Hinnant0bb0a7c2013-07-29 19:05:472100#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 Hinnantbc8d3f92010-05-11 19:42:162105 __node_holder __h = __construct_node(__x);
2106 iterator __r = __node_insert_multi(__p, __h.get());
2107 __h.release();
2108 return __r;
2109}
2110
Eric Fiselier2960ae22016-02-11 11:59:442111#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:162112
2113template <class _Tp, class _Hash, class _Equal, class _Alloc>
2114void
2115__hash_table<_Tp, _Hash, _Equal, _Alloc>::rehash(size_type __n)
2116{
Howard Hinnant7a445152012-07-06 17:31:142117 if (__n == 1)
2118 __n = 2;
2119 else if (__n & (__n - 1))
2120 __n = __next_prime(__n);
Howard Hinnantbc8d3f92010-05-11 19:42:162121 size_type __bc = bucket_count();
2122 if (__n > __bc)
2123 __rehash(__n);
Howard Hinnant7a445152012-07-06 17:31:142124 else if (__n < __bc)
Howard Hinnantbc8d3f92010-05-11 19:42:162125 {
Howard Hinnant0949eed2011-06-30 21:18:192126 __n = _VSTD::max<size_type>
Howard Hinnantbc8d3f92010-05-11 19:42:162127 (
2128 __n,
Eric Fiselier57947ca2015-02-02 21:31:482129 __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 Hinnantbc8d3f92010-05-11 19:42:162131 );
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 Hinnant39213642013-07-23 22:01:582141#if _LIBCPP_DEBUG_LEVEL >= 2
2142 __get_db()->__invalidate_all(this);
2143#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:162144 __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 Hinnant7a6b7ce2013-06-22 15:21:292152 __node_pointer __pp(static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first())));
Howard Hinnantbc8d3f92010-05-11 19:42:162153 __node_pointer __cp = __pp->__next_;
2154 if (__cp != nullptr)
2155 {
Howard Hinnant7a445152012-07-06 17:31:142156 size_type __chash = __constrain_hash(__cp->__hash_, __nbc);
Howard Hinnantbc8d3f92010-05-11 19:42:162157 __bucket_list_[__chash] = __pp;
2158 size_type __phash = __chash;
2159 for (__pp = __cp, __cp = __cp->__next_; __cp != nullptr;
2160 __cp = __pp->__next_)
2161 {
Howard Hinnant7a445152012-07-06 17:31:142162 __chash = __constrain_hash(__cp->__hash_, __nbc);
Howard Hinnantbc8d3f92010-05-11 19:42:162163 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 Hinnant324bb032010-08-22 00:02:432183
Howard Hinnantbc8d3f92010-05-11 19:42:162184 }
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 Hinnant7a445152012-07-06 17:31:142200 size_t __chash = __constrain_hash(__hash, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:162201 __node_pointer __nd = __bucket_list_[__chash];
2202 if (__nd != nullptr)
2203 {
2204 for (__nd = __nd->__next_; __nd != nullptr &&
Eric Fiselier41aafc22016-07-17 22:04:572205 (__nd->__hash_ == __hash
2206 || __constrain_hash(__nd->__hash_, __bc) == __chash);
Howard Hinnantbc8d3f92010-05-11 19:42:162207 __nd = __nd->__next_)
2208 {
Kwasi Mensah95a22db2016-07-08 15:34:282209 if ((__nd->__hash_ == __hash) && key_eq()(__nd->__value_, __k))
Howard Hinnant39213642013-07-23 22:01:582210#if _LIBCPP_DEBUG_LEVEL >= 2
2211 return iterator(__nd, this);
2212#else
Howard Hinnantbc8d3f92010-05-11 19:42:162213 return iterator(__nd);
Howard Hinnant39213642013-07-23 22:01:582214#endif
Howard Hinnantbc8d3f92010-05-11 19:42:162215 }
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 Hinnant7a445152012-07-06 17:31:142230 size_t __chash = __constrain_hash(__hash, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:162231 __node_const_pointer __nd = __bucket_list_[__chash];
2232 if (__nd != nullptr)
2233 {
2234 for (__nd = __nd->__next_; __nd != nullptr &&
Eric Fiselier41aafc22016-07-17 22:04:572235 (__hash == __nd->__hash_ || __constrain_hash(__nd->__hash_, __bc) == __chash);
Howard Hinnantbc8d3f92010-05-11 19:42:162236 __nd = __nd->__next_)
2237 {
Kwasi Mensah95a22db2016-07-08 15:34:282238 if ((__nd->__hash_ == __hash) && key_eq()(__nd->__value_, __k))
Howard Hinnant39213642013-07-23 22:01:582239#if _LIBCPP_DEBUG_LEVEL >= 2
2240 return const_iterator(__nd, this);
2241#else
Howard Hinnantbc8d3f92010-05-11 19:42:162242 return const_iterator(__nd);
Howard Hinnant39213642013-07-23 22:01:582243#endif
Howard Hinnantbc8d3f92010-05-11 19:42:162244 }
2245 }
Howard Hinnant324bb032010-08-22 00:02:432246
Howard Hinnantbc8d3f92010-05-11 19:42:162247 }
2248 return end();
2249}
2250
Eric Fiselier2960ae22016-02-11 11:59:442251#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:162252
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 Fiselier2960ae22016-02-11 11:59:442258 static_assert(!__is_hash_value_type<_Args...>::value,
2259 "Construct cannot be called with a hash value type");
Howard Hinnantbc8d3f92010-05-11 19:42:162260 __node_allocator& __na = __node_alloc();
Howard Hinnant99968442011-11-29 18:15:502261 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselier2960ae22016-02-11 11:59:442262 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:162263 __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 Fiselier2960ae22016-02-11 11:59:442270template <class _First, class ..._Rest>
Howard Hinnantbc8d3f92010-05-11 19:42:162271typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Eric Fiselier2960ae22016-02-11 11:59:442272__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node_hash(
2273 size_t __hash, _First&& __f, _Rest&& ...__rest)
Howard Hinnantbc8d3f92010-05-11 19:42:162274{
Eric Fiselier2960ae22016-02-11 11:59:442275 static_assert(!__is_hash_value_type<_First, _Rest...>::value,
2276 "Construct cannot be called with a hash value type");
Howard Hinnantbc8d3f92010-05-11 19:42:162277 __node_allocator& __na = __node_alloc();
Howard Hinnant99968442011-11-29 18:15:502278 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselier2960ae22016-02-11 11:59:442279 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_),
2280 _VSTD::forward<_First>(__f),
2281 _VSTD::forward<_Rest>(__rest)...);
Howard Hinnantbc8d3f92010-05-11 19:42:162282 __h.get_deleter().__value_constructed = true;
2283 __h->__hash_ = __hash;
2284 __h->__next_ = nullptr;
Howard Hinnant9a894d92013-08-22 18:29:502285 return __h;
Howard Hinnantbc8d3f92010-05-11 19:42:162286}
2287
Eric Fiselier2960ae22016-02-11 11:59:442288#else // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:162289
2290template <class _Tp, class _Hash, class _Equal, class _Alloc>
2291typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Eric Fiselier2960ae22016-02-11 11:59:442292__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(const __container_value_type& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:162293{
2294 __node_allocator& __na = __node_alloc();
Howard Hinnant99968442011-11-29 18:15:502295 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselier2960ae22016-02-11 11:59:442296 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:162297 __h.get_deleter().__value_constructed = true;
2298 __h->__hash_ = hash_function()(__h->__value_);
2299 __h->__next_ = nullptr;
Dimitry Andric89663502015-08-19 06:43:332300 return _LIBCPP_EXPLICIT_MOVE(__h); // explicitly moved for C++03
Howard Hinnantbc8d3f92010-05-11 19:42:162301}
2302
Howard Hinnantbc8d3f92010-05-11 19:42:162303template <class _Tp, class _Hash, class _Equal, class _Alloc>
2304typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Eric Fiselier2960ae22016-02-11 11:59:442305__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node_hash(size_t __hash,
2306 const __container_value_type& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:162307{
2308 __node_allocator& __na = __node_alloc();
Howard Hinnant99968442011-11-29 18:15:502309 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselier2960ae22016-02-11 11:59:442310 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:162311 __h.get_deleter().__value_constructed = true;
2312 __h->__hash_ = __hash;
2313 __h->__next_ = nullptr;
Dimitry Andric89663502015-08-19 06:43:332314 return _LIBCPP_EXPLICIT_MOVE(__h); // explicitly moved for C++03
Howard Hinnantbc8d3f92010-05-11 19:42:162315}
2316
Eric Fiselier2960ae22016-02-11 11:59:442317#endif // _LIBCPP_CXX03_LANG
2318
Howard Hinnantbc8d3f92010-05-11 19:42:162319template <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 Hinnant7a6b7ce2013-06-22 15:21:292323 __node_pointer __np = __p.__node_;
Howard Hinnant39213642013-07-23 22:01:582324#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 Hinnantbc8d3f92010-05-11 19:42:162332 iterator __r(__np);
Howard Hinnant39213642013-07-23 22:01:582333#endif
Howard Hinnantbc8d3f92010-05-11 19:42:162334 ++__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 Hinnant39213642013-07-23 22:01:582344#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 Hinnantbc8d3f92010-05-11 19:42:162352 for (const_iterator __p = __first; __first != __last; __p = __first)
2353 {
2354 ++__first;
2355 erase(__p);
2356 }
Howard Hinnant7a6b7ce2013-06-22 15:21:292357 __node_pointer __np = __last.__node_;
Howard Hinnant39213642013-07-23 22:01:582358#if _LIBCPP_DEBUG_LEVEL >= 2
2359 return iterator (__np, this);
2360#else
Howard Hinnantbc8d3f92010-05-11 19:42:162361 return iterator (__np);
Howard Hinnant39213642013-07-23 22:01:582362#endif
Howard Hinnantbc8d3f92010-05-11 19:42:162363}
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 Hinnant5f2f14c2011-06-04 18:54:242398__hash_table<_Tp, _Hash, _Equal, _Alloc>::remove(const_iterator __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:162399{
2400 // current node
Howard Hinnant7a6b7ce2013-06-22 15:21:292401 __node_pointer __cn = __p.__node_;
Howard Hinnantbc8d3f92010-05-11 19:42:162402 size_type __bc = bucket_count();
Howard Hinnant7a445152012-07-06 17:31:142403 size_t __chash = __constrain_hash(__cn->__hash_, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:162404 // 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 Hinnant7a6b7ce2013-06-22 15:21:292411 if (__pn == static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()))
2412 || __constrain_hash(__pn->__hash_, __bc) != __chash)
Howard Hinnantbc8d3f92010-05-11 19:42:162413 {
Howard Hinnant7a445152012-07-06 17:31:142414 if (__cn->__next_ == nullptr || __constrain_hash(__cn->__next_->__hash_, __bc) != __chash)
Howard Hinnantbc8d3f92010-05-11 19:42:162415 __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 Hinnant7a445152012-07-06 17:31:142420 size_t __nhash = __constrain_hash(__cn->__next_->__hash_, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:162421 if (__nhash != __chash)
2422 __bucket_list_[__nhash] = __pn;
2423 }
2424 // remove __cn
2425 __pn->__next_ = __cn->__next_;
2426 __cn->__next_ = nullptr;
2427 --size();
Howard Hinnant39213642013-07-23 22:01:582428#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 Hinnant99968442011-11-29 18:15:502443 return __node_holder(__cn, _Dp(__node_alloc(), true));
Howard Hinnantbc8d3f92010-05-11 19:42:162444}
2445
2446template <class _Tp, class _Hash, class _Equal, class _Alloc>
2447template <class _Key>
Evgeniy Stepanova3b25f82015-11-07 01:22:132448inline
Howard Hinnantbc8d3f92010-05-11 19:42:162449typename __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 Fiselier692177d2015-07-18 20:40:462545#if _LIBCPP_STD_VER <= 11
Howard Hinnant5f2f14c2011-06-04 18:54:242546 _NOEXCEPT_(
Marshall Clow7d914d12015-07-13 20:04:562547 __is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value
Marshall Clow7d914d12015-07-13 20:04:562548 && (!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 Clow7d914d12015-07-13 20:04:562552 )
Eric Fiselier692177d2015-07-18 20:40:462553#else
2554 _NOEXCEPT_(__is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value)
2555#endif
Howard Hinnantbc8d3f92010-05-11 19:42:162556{
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 Hinnant0949eed2011-06-30 21:18:192562 _VSTD::swap(__bucket_list_.get_deleter().size(), __u.__bucket_list_.get_deleter().size());
Marshall Clow7d914d12015-07-13 20:04:562563 __swap_allocator(__bucket_list_.get_deleter().__alloc(),
Howard Hinnantbc8d3f92010-05-11 19:42:162564 __u.__bucket_list_.get_deleter().__alloc());
Marshall Clow7d914d12015-07-13 20:04:562565 __swap_allocator(__node_alloc(), __u.__node_alloc());
Howard Hinnant0949eed2011-06-30 21:18:192566 _VSTD::swap(__p1_.first().__next_, __u.__p1_.first().__next_);
Howard Hinnantbc8d3f92010-05-11 19:42:162567 __p2_.swap(__u.__p2_);
2568 __p3_.swap(__u.__p3_);
2569 if (size() > 0)
Howard Hinnant7a445152012-07-06 17:31:142570 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash_, bucket_count())] =
Howard Hinnant7a6b7ce2013-06-22 15:21:292571 static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
Howard Hinnantbc8d3f92010-05-11 19:42:162572 if (__u.size() > 0)
Howard Hinnant7a445152012-07-06 17:31:142573 __u.__bucket_list_[__constrain_hash(__u.__p1_.first().__next_->__hash_, __u.bucket_count())] =
Howard Hinnant7a6b7ce2013-06-22 15:21:292574 static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__u.__p1_.first()));
Howard Hinnant39213642013-07-23 22:01:582575#if _LIBCPP_DEBUG_LEVEL >= 2
2576 __get_db()->swap(this, &__u);
2577#endif
Howard Hinnantbc8d3f92010-05-11 19:42:162578}
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 Hinnant0bb0a7c2013-07-29 19:05:472584 _LIBCPP_ASSERT(__n < bucket_count(),
2585 "unordered container::bucket_size(n) called with n >= bucket_count()");
Howard Hinnantbc8d3f92010-05-11 19:42:162586 __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 Hinnant7a445152012-07-06 17:31:142592 __constrain_hash(__np->__hash_, __bc) == __n;
Howard Hinnantbc8d3f92010-05-11 19:42:162593 __np = __np->__next_, ++__r)
2594 ;
2595 }
2596 return __r;
2597}
2598
Howard Hinnant5f2f14c2011-06-04 18:54:242599template <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 Hinnant39213642013-07-23 22:01:582609#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 Hinnantbc8d3f92010-05-11 19:42:162640_LIBCPP_END_NAMESPACE_STD
2641
2642#endif // _LIBCPP__HASH_TABLE