blob: 87709a3c5faedb96fe25d9fe10b2015e53e10e21 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP__HASH_TABLE
12#define _LIBCPP__HASH_TABLE
13
14#include <__config>
15#include <initializer_list>
16#include <memory>
17#include <iterator>
18#include <algorithm>
19#include <cmath>
Eric Fiselier774c7c52016-02-10 20:46:23 +000020#include <utility>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000021
Howard Hinnant66c6f972011-11-29 16:45:27 +000022#include <__undef_min_max>
Saleem Abdulrasoolf1b30c42015-02-13 22:15:32 +000023#include <__undef___deallocate>
Howard Hinnant66c6f972011-11-29 16:45:27 +000024
Eric Fiselierb9536102014-08-10 23:53:08 +000025#include <__debug>
Howard Hinnant8b00e6c2013-08-02 00:26:35 +000026
Howard Hinnant08e17472011-10-17 20:05:10 +000027#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000028#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +000029#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000030
31_LIBCPP_BEGIN_NAMESPACE_STD
32
Eric Fiselier2960ae22016-02-11 11:59:44 +000033
34#ifndef _LIBCPP_CXX03_LANG
35template <class _Key, class _Tp>
36union __hash_value_type;
37#else
38template <class _Key, class _Tp>
39struct __hash_value_type;
40#endif
41
42#ifndef _LIBCPP_CXX03_LANG
43template <class _Tp>
44struct __is_hash_value_type_imp : false_type {};
45
46template <class _Key, class _Value>
47struct __is_hash_value_type_imp<__hash_value_type<_Key, _Value>> : true_type {};
48
49template <class ..._Args>
50struct __is_hash_value_type : false_type {};
51
52template <class _One>
53struct __is_hash_value_type<_One> : __is_hash_value_type_imp<typename __uncvref<_One>::type> {};
54#endif
55
Howard Hinnant83eade62013-03-06 23:30:19 +000056_LIBCPP_FUNC_VIS
Howard Hinnant2b1b2d42011-06-14 19:58:17 +000057size_t __next_prime(size_t __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000058
59template <class _NodePtr>
60struct __hash_node_base
61{
62 typedef __hash_node_base __first_node;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000063
Howard Hinnantdf85e572011-02-27 18:02:02 +000064 _NodePtr __next_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000065
Howard Hinnant5f2f14c2011-06-04 18:54:24 +000066 _LIBCPP_INLINE_VISIBILITY __hash_node_base() _NOEXCEPT : __next_(nullptr) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000067};
68
69template <class _Tp, class _VoidPtr>
70struct __hash_node
71 : public __hash_node_base
72 <
Eric Fiselier5cf84e02015-12-30 21:52:00 +000073 typename __rebind_pointer<_VoidPtr, __hash_node<_Tp, _VoidPtr> >::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000074 >
75{
Eric Fiselier774c7c52016-02-10 20:46:23 +000076 typedef _Tp __node_value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000077
78 size_t __hash_;
Eric Fiselier774c7c52016-02-10 20:46:23 +000079 __node_value_type __value_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000080};
81
Howard Hinnant7a445152012-07-06 17:31:14 +000082inline _LIBCPP_INLINE_VISIBILITY
83bool
Eric Fiselier57947ca2015-02-02 21:31:48 +000084__is_hash_power2(size_t __bc)
Howard Hinnant7a445152012-07-06 17:31:14 +000085{
86 return __bc > 2 && !(__bc & (__bc - 1));
87}
88
89inline _LIBCPP_INLINE_VISIBILITY
90size_t
91__constrain_hash(size_t __h, size_t __bc)
92{
93 return !(__bc & (__bc - 1)) ? __h & (__bc - 1) : __h % __bc;
94}
95
96inline _LIBCPP_INLINE_VISIBILITY
97size_t
Eric Fiselier57947ca2015-02-02 21:31:48 +000098__next_hash_pow2(size_t __n)
Howard Hinnant7a445152012-07-06 17:31:14 +000099{
100 return size_t(1) << (std::numeric_limits<size_t>::digits - __clz(__n-1));
101}
102
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000103template <class _Tp, class _Hash, class _Equal, class _Alloc> class __hash_table;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000104
105template <class _NodePtr> class _LIBCPP_TYPE_VIS_ONLY __hash_iterator;
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000106template <class _ConstNodePtr> class _LIBCPP_TYPE_VIS_ONLY __hash_const_iterator;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000107template <class _NodePtr> class _LIBCPP_TYPE_VIS_ONLY __hash_local_iterator;
108template <class _ConstNodePtr> class _LIBCPP_TYPE_VIS_ONLY __hash_const_local_iterator;
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000109template <class _HashIterator> class _LIBCPP_TYPE_VIS_ONLY __hash_map_iterator;
110template <class _HashIterator> class _LIBCPP_TYPE_VIS_ONLY __hash_map_const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000111
Eric Fiselier774c7c52016-02-10 20:46:23 +0000112template <class _Tp>
113struct __key_value_types {
114 static_assert(!is_reference<_Tp>::value && !is_const<_Tp>::value, "");
115 typedef _Tp key_type;
116 typedef _Tp __node_value_type;
117 typedef _Tp __container_value_type;
118 static const bool __is_map = false;
Eric Fiselier2960ae22016-02-11 11:59:44 +0000119
120 _LIBCPP_INLINE_VISIBILITY
121 static key_type const& __get_key(_Tp const& __v) {
122 return __v;
123 }
124 _LIBCPP_INLINE_VISIBILITY
125 static __container_value_type const& __get_value(__node_value_type const& __v) {
126 return __v;
127 }
128 _LIBCPP_INLINE_VISIBILITY
129 static __container_value_type* __get_ptr(__node_value_type& __n) {
130 return _VSTD::addressof(__n);
131 }
132#ifndef _LIBCPP_CXX03_LANG
133 _LIBCPP_INLINE_VISIBILITY
134 static __container_value_type&& __move(__node_value_type& __v) {
135 return _VSTD::move(__v);
136 }
137#endif
Eric Fiselier774c7c52016-02-10 20:46:23 +0000138};
139
140template <class _Key, class _Tp>
141struct __key_value_types<__hash_value_type<_Key, _Tp> > {
142 typedef _Key key_type;
143 typedef _Tp mapped_type;
144 typedef __hash_value_type<_Key, _Tp> __node_value_type;
145 typedef pair<const _Key, _Tp> __container_value_type;
Eric Fiselier2960ae22016-02-11 11:59:44 +0000146 typedef pair<_Key, _Tp> __nc_value_type;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000147 typedef __container_value_type __map_value_type;
148 static const bool __is_map = true;
Eric Fiselier2960ae22016-02-11 11:59:44 +0000149
150 _LIBCPP_INLINE_VISIBILITY
151 static key_type const& __get_key(__container_value_type const& __v) {
152 return __v.first;
153 }
154
155 template <class _Up>
156 _LIBCPP_INLINE_VISIBILITY
157 static typename enable_if<__is_same_uncvref<_Up, __node_value_type>::value,
158 __container_value_type const&>::type
159 __get_value(_Up& __t) {
160 return __t.__cc;
161 }
162
163 template <class _Up>
164 _LIBCPP_INLINE_VISIBILITY
165 static typename enable_if<__is_same_uncvref<_Up, __container_value_type>::value,
166 __container_value_type const&>::type
167 __get_value(_Up& __t) {
168 return __t;
169 }
170
171 _LIBCPP_INLINE_VISIBILITY
172 static __container_value_type* __get_ptr(__node_value_type& __n) {
173 return _VSTD::addressof(__n.__cc);
174 }
175#ifndef _LIBCPP_CXX03_LANG
176 _LIBCPP_INLINE_VISIBILITY
177 static __nc_value_type&& __move(__node_value_type& __v) {
178 return _VSTD::move(__v.__nc);
179 }
180#endif
181
Eric Fiselier774c7c52016-02-10 20:46:23 +0000182};
183
184template <class _Tp, class _AllocPtr, class _KVTypes = __key_value_types<_Tp>,
185 bool = _KVTypes::__is_map>
186struct __map_pointer_types {};
187
188template <class _Tp, class _AllocPtr, class _KVTypes>
189struct __map_pointer_types<_Tp, _AllocPtr, _KVTypes, true> {
190 typedef typename _KVTypes::__map_value_type _Mv;
191 typedef typename __rebind_pointer<_AllocPtr, _Mv>::type
192 __map_value_type_pointer;
193 typedef typename __rebind_pointer<_AllocPtr, const _Mv>::type
194 __const_map_value_type_pointer;
195};
196
197template <class _NodePtr, class _NodeT = typename pointer_traits<_NodePtr>::element_type>
198struct __hash_node_types;
199
200template <class _NodePtr, class _Tp, class _VoidPtr>
201struct __hash_node_types<_NodePtr, __hash_node<_Tp, _VoidPtr> >
202 : public __key_value_types<_Tp>, __map_pointer_types<_Tp, _VoidPtr>
203
204{
205 typedef __key_value_types<_Tp> __base;
206
207public:
208 typedef ptrdiff_t difference_type;
209 typedef size_t size_type;
210
211 typedef typename __rebind_pointer<_NodePtr, void>::type __void_pointer;
212
213 typedef typename pointer_traits<_NodePtr>::element_type __node_type;
214 typedef _NodePtr __node_pointer;
215
216 typedef __hash_node_base<__node_pointer> __node_base_type;
217 typedef typename __rebind_pointer<_NodePtr, __node_base_type>::type
218 __node_base_pointer;
219
220 typedef _Tp __node_value_type;
221 typedef typename __rebind_pointer<_VoidPtr, __node_value_type>::type
222 __node_value_type_pointer;
223 typedef typename __rebind_pointer<_VoidPtr, const __node_value_type>::type
224 __const_node_value_type_pointer;
225private:
226 static_assert(!is_const<__node_type>::value,
227 "_NodePtr should never be a pointer to const");
228 static_assert((is_same<typename pointer_traits<_VoidPtr>::element_type, void>::value),
229 "_VoidPtr does not point to unqualified void type");
230 static_assert((is_same<typename __rebind_pointer<_VoidPtr, __node_type>::type,
231 _NodePtr>::value), "_VoidPtr does not rebind to _NodePtr.");
232};
233
234
235
236template <class _HashIterator>
237struct __hash_node_types_from_iterator;
238template <class _NodePtr>
239struct __hash_node_types_from_iterator<__hash_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
240template <class _NodePtr>
241struct __hash_node_types_from_iterator<__hash_const_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
242template <class _NodePtr>
243struct __hash_node_types_from_iterator<__hash_local_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
244template <class _NodePtr>
245struct __hash_node_types_from_iterator<__hash_const_local_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
246
247
248template <class _NodeValueTp, class _VoidPtr>
249struct __make_hash_node_types {
250 typedef __hash_node<_NodeValueTp, _VoidPtr> _NodeTp;
251 typedef typename __rebind_pointer<_VoidPtr, _NodeTp>::type _NodePtr;
252 typedef __hash_node_types<_NodePtr> type;
253};
254
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000255template <class _NodePtr>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000256class _LIBCPP_TYPE_VIS_ONLY __hash_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000257{
Eric Fiselier774c7c52016-02-10 20:46:23 +0000258 typedef __hash_node_types<_NodePtr> _NodeTypes;
259 typedef _NodePtr __node_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000260
261 __node_pointer __node_;
262
263public:
Eric Fiselier774c7c52016-02-10 20:46:23 +0000264 typedef forward_iterator_tag iterator_category;
265 typedef typename _NodeTypes::__node_value_type value_type;
266 typedef typename _NodeTypes::difference_type difference_type;
267 typedef value_type& reference;
268 typedef typename _NodeTypes::__node_value_type_pointer pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000269
Howard Hinnant39213642013-07-23 22:01:58 +0000270 _LIBCPP_INLINE_VISIBILITY __hash_iterator() _NOEXCEPT
Marshall Clow193ef032013-08-07 21:30:44 +0000271#if _LIBCPP_STD_VER > 11
272 : __node_(nullptr)
273#endif
Howard Hinnant39213642013-07-23 22:01:58 +0000274 {
275#if _LIBCPP_DEBUG_LEVEL >= 2
276 __get_db()->__insert_i(this);
277#endif
278 }
279
280#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000281
Howard Hinnant99acc502010-09-21 17:32:39 +0000282 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000283 __hash_iterator(const __hash_iterator& __i)
284 : __node_(__i.__node_)
285 {
286 __get_db()->__iterator_copy(this, &__i);
287 }
288
Howard Hinnant99acc502010-09-21 17:32:39 +0000289 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000290 ~__hash_iterator()
291 {
292 __get_db()->__erase_i(this);
293 }
294
295 _LIBCPP_INLINE_VISIBILITY
296 __hash_iterator& operator=(const __hash_iterator& __i)
297 {
298 if (this != &__i)
299 {
300 __get_db()->__iterator_copy(this, &__i);
301 __node_ = __i.__node_;
302 }
303 return *this;
304 }
305
306#endif // _LIBCPP_DEBUG_LEVEL >= 2
307
308 _LIBCPP_INLINE_VISIBILITY
309 reference operator*() const
310 {
311#if _LIBCPP_DEBUG_LEVEL >= 2
312 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
313 "Attempted to dereference a non-dereferenceable unordered container iterator");
314#endif
315 return __node_->__value_;
316 }
317 _LIBCPP_INLINE_VISIBILITY
318 pointer operator->() const
319 {
320#if _LIBCPP_DEBUG_LEVEL >= 2
321 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
322 "Attempted to dereference a non-dereferenceable unordered container iterator");
323#endif
324 return pointer_traits<pointer>::pointer_to(__node_->__value_);
325 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000326
Howard Hinnant99acc502010-09-21 17:32:39 +0000327 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000328 __hash_iterator& operator++()
329 {
Howard Hinnant39213642013-07-23 22:01:58 +0000330#if _LIBCPP_DEBUG_LEVEL >= 2
331 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
332 "Attempted to increment non-incrementable unordered container iterator");
333#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000334 __node_ = __node_->__next_;
335 return *this;
336 }
337
Howard Hinnant99acc502010-09-21 17:32:39 +0000338 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000339 __hash_iterator operator++(int)
340 {
341 __hash_iterator __t(*this);
342 ++(*this);
343 return __t;
344 }
345
Howard Hinnant99acc502010-09-21 17:32:39 +0000346 friend _LIBCPP_INLINE_VISIBILITY
347 bool operator==(const __hash_iterator& __x, const __hash_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58 +0000348 {
Howard Hinnant39213642013-07-23 22:01:58 +0000349 return __x.__node_ == __y.__node_;
350 }
Howard Hinnant99acc502010-09-21 17:32:39 +0000351 friend _LIBCPP_INLINE_VISIBILITY
352 bool operator!=(const __hash_iterator& __x, const __hash_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58 +0000353 {return !(__x == __y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000354
355private:
Howard Hinnant39213642013-07-23 22:01:58 +0000356#if _LIBCPP_DEBUG_LEVEL >= 2
357 _LIBCPP_INLINE_VISIBILITY
358 __hash_iterator(__node_pointer __node, const void* __c) _NOEXCEPT
359 : __node_(__node)
360 {
361 __get_db()->__insert_ic(this, __c);
362 }
363#else
Howard Hinnant99acc502010-09-21 17:32:39 +0000364 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000365 __hash_iterator(__node_pointer __node) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000366 : __node_(__node)
367 {}
Howard Hinnant39213642013-07-23 22:01:58 +0000368#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000369
370 template <class, class, class, class> friend class __hash_table;
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000371 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_const_iterator;
372 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_map_iterator;
373 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_map;
374 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_multimap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000375};
376
Eric Fiselier774c7c52016-02-10 20:46:23 +0000377template <class _NodePtr>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000378class _LIBCPP_TYPE_VIS_ONLY __hash_const_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000379{
Eric Fiselier774c7c52016-02-10 20:46:23 +0000380 static_assert(!is_const<typename pointer_traits<_NodePtr>::element_type>::value, "");
381 typedef __hash_node_types<_NodePtr> _NodeTypes;
382 typedef _NodePtr __node_pointer;
Eric Fiselier0493d022016-02-18 00:20:34 +0000383
Eric Fiselier774c7c52016-02-10 20:46:23 +0000384 __node_pointer __node_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000385
386public:
Eric Fiselier0493d022016-02-18 00:20:34 +0000387 typedef __hash_iterator<_NodePtr> __non_const_iterator;
388
Eric Fiselier774c7c52016-02-10 20:46:23 +0000389 typedef forward_iterator_tag iterator_category;
390 typedef typename _NodeTypes::__node_value_type value_type;
391 typedef typename _NodeTypes::difference_type difference_type;
392 typedef const value_type& reference;
393 typedef typename _NodeTypes::__const_node_value_type_pointer pointer;
394
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000395
Howard Hinnant39213642013-07-23 22:01:58 +0000396 _LIBCPP_INLINE_VISIBILITY __hash_const_iterator() _NOEXCEPT
Marshall Clow193ef032013-08-07 21:30:44 +0000397#if _LIBCPP_STD_VER > 11
398 : __node_(nullptr)
399#endif
Howard Hinnant39213642013-07-23 22:01:58 +0000400 {
401#if _LIBCPP_DEBUG_LEVEL >= 2
402 __get_db()->__insert_i(this);
403#endif
404 }
Howard Hinnant99acc502010-09-21 17:32:39 +0000405 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000406 __hash_const_iterator(const __non_const_iterator& __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000407 : __node_(__x.__node_)
Howard Hinnant39213642013-07-23 22:01:58 +0000408 {
409#if _LIBCPP_DEBUG_LEVEL >= 2
410 __get_db()->__iterator_copy(this, &__x);
411#endif
412 }
413
414#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000415
Howard Hinnant99acc502010-09-21 17:32:39 +0000416 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000417 __hash_const_iterator(const __hash_const_iterator& __i)
418 : __node_(__i.__node_)
419 {
420 __get_db()->__iterator_copy(this, &__i);
421 }
422
Howard Hinnant99acc502010-09-21 17:32:39 +0000423 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000424 ~__hash_const_iterator()
425 {
426 __get_db()->__erase_i(this);
427 }
428
429 _LIBCPP_INLINE_VISIBILITY
430 __hash_const_iterator& operator=(const __hash_const_iterator& __i)
431 {
432 if (this != &__i)
433 {
434 __get_db()->__iterator_copy(this, &__i);
435 __node_ = __i.__node_;
436 }
437 return *this;
438 }
439
440#endif // _LIBCPP_DEBUG_LEVEL >= 2
441
442 _LIBCPP_INLINE_VISIBILITY
443 reference operator*() const
444 {
445#if _LIBCPP_DEBUG_LEVEL >= 2
446 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
447 "Attempted to dereference a non-dereferenceable unordered container const_iterator");
448#endif
449 return __node_->__value_;
450 }
451 _LIBCPP_INLINE_VISIBILITY
452 pointer operator->() const
453 {
454#if _LIBCPP_DEBUG_LEVEL >= 2
455 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
456 "Attempted to dereference a non-dereferenceable unordered container const_iterator");
457#endif
458 return pointer_traits<pointer>::pointer_to(__node_->__value_);
459 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000460
Howard Hinnant99acc502010-09-21 17:32:39 +0000461 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000462 __hash_const_iterator& operator++()
463 {
Howard Hinnant39213642013-07-23 22:01:58 +0000464#if _LIBCPP_DEBUG_LEVEL >= 2
465 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
466 "Attempted to increment non-incrementable unordered container const_iterator");
467#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000468 __node_ = __node_->__next_;
469 return *this;
470 }
471
Howard Hinnant99acc502010-09-21 17:32:39 +0000472 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000473 __hash_const_iterator operator++(int)
474 {
475 __hash_const_iterator __t(*this);
476 ++(*this);
477 return __t;
478 }
479
Howard Hinnant99acc502010-09-21 17:32:39 +0000480 friend _LIBCPP_INLINE_VISIBILITY
481 bool operator==(const __hash_const_iterator& __x, const __hash_const_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58 +0000482 {
Howard Hinnant39213642013-07-23 22:01:58 +0000483 return __x.__node_ == __y.__node_;
484 }
Howard Hinnant99acc502010-09-21 17:32:39 +0000485 friend _LIBCPP_INLINE_VISIBILITY
486 bool operator!=(const __hash_const_iterator& __x, const __hash_const_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58 +0000487 {return !(__x == __y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000488
489private:
Howard Hinnant39213642013-07-23 22:01:58 +0000490#if _LIBCPP_DEBUG_LEVEL >= 2
491 _LIBCPP_INLINE_VISIBILITY
492 __hash_const_iterator(__node_pointer __node, const void* __c) _NOEXCEPT
493 : __node_(__node)
494 {
495 __get_db()->__insert_ic(this, __c);
496 }
497#else
Howard Hinnant99acc502010-09-21 17:32:39 +0000498 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000499 __hash_const_iterator(__node_pointer __node) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000500 : __node_(__node)
501 {}
Howard Hinnant39213642013-07-23 22:01:58 +0000502#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000503
504 template <class, class, class, class> friend class __hash_table;
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000505 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_map_const_iterator;
506 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_map;
507 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_multimap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000508};
509
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000510template <class _NodePtr>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000511class _LIBCPP_TYPE_VIS_ONLY __hash_local_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000512{
Eric Fiselier774c7c52016-02-10 20:46:23 +0000513 typedef __hash_node_types<_NodePtr> _NodeTypes;
514 typedef _NodePtr __node_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000515
516 __node_pointer __node_;
517 size_t __bucket_;
518 size_t __bucket_count_;
519
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000520public:
521 typedef forward_iterator_tag iterator_category;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000522 typedef typename _NodeTypes::__node_value_type value_type;
523 typedef typename _NodeTypes::difference_type difference_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000524 typedef value_type& reference;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000525 typedef typename _NodeTypes::__node_value_type_pointer pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000526
Howard Hinnant39213642013-07-23 22:01:58 +0000527 _LIBCPP_INLINE_VISIBILITY __hash_local_iterator() _NOEXCEPT
528 {
529#if _LIBCPP_DEBUG_LEVEL >= 2
530 __get_db()->__insert_i(this);
531#endif
532 }
533
534#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000535
Howard Hinnant99acc502010-09-21 17:32:39 +0000536 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000537 __hash_local_iterator(const __hash_local_iterator& __i)
538 : __node_(__i.__node_),
539 __bucket_(__i.__bucket_),
540 __bucket_count_(__i.__bucket_count_)
541 {
542 __get_db()->__iterator_copy(this, &__i);
543 }
544
Howard Hinnant99acc502010-09-21 17:32:39 +0000545 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000546 ~__hash_local_iterator()
547 {
548 __get_db()->__erase_i(this);
549 }
550
551 _LIBCPP_INLINE_VISIBILITY
552 __hash_local_iterator& operator=(const __hash_local_iterator& __i)
553 {
554 if (this != &__i)
555 {
556 __get_db()->__iterator_copy(this, &__i);
557 __node_ = __i.__node_;
558 __bucket_ = __i.__bucket_;
559 __bucket_count_ = __i.__bucket_count_;
560 }
561 return *this;
562 }
563
564#endif // _LIBCPP_DEBUG_LEVEL >= 2
565
566 _LIBCPP_INLINE_VISIBILITY
567 reference operator*() const
568 {
569#if _LIBCPP_DEBUG_LEVEL >= 2
570 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
571 "Attempted to dereference a non-dereferenceable unordered container local_iterator");
572#endif
573 return __node_->__value_;
574 }
575 _LIBCPP_INLINE_VISIBILITY
576 pointer operator->() const
577 {
578#if _LIBCPP_DEBUG_LEVEL >= 2
579 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
580 "Attempted to dereference a non-dereferenceable unordered container local_iterator");
581#endif
582 return pointer_traits<pointer>::pointer_to(__node_->__value_);
583 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000584
Howard Hinnant99acc502010-09-21 17:32:39 +0000585 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000586 __hash_local_iterator& operator++()
587 {
Howard Hinnant39213642013-07-23 22:01:58 +0000588#if _LIBCPP_DEBUG_LEVEL >= 2
589 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
590 "Attempted to increment non-incrementable unordered container local_iterator");
591#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000592 __node_ = __node_->__next_;
Howard Hinnant7a445152012-07-06 17:31:14 +0000593 if (__node_ != nullptr && __constrain_hash(__node_->__hash_, __bucket_count_) != __bucket_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000594 __node_ = nullptr;
595 return *this;
596 }
597
Howard Hinnant99acc502010-09-21 17:32:39 +0000598 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000599 __hash_local_iterator operator++(int)
600 {
601 __hash_local_iterator __t(*this);
602 ++(*this);
603 return __t;
604 }
605
Howard Hinnant99acc502010-09-21 17:32:39 +0000606 friend _LIBCPP_INLINE_VISIBILITY
607 bool operator==(const __hash_local_iterator& __x, const __hash_local_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58 +0000608 {
Howard Hinnant39213642013-07-23 22:01:58 +0000609 return __x.__node_ == __y.__node_;
610 }
Howard Hinnant99acc502010-09-21 17:32:39 +0000611 friend _LIBCPP_INLINE_VISIBILITY
612 bool operator!=(const __hash_local_iterator& __x, const __hash_local_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58 +0000613 {return !(__x == __y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000614
615private:
Howard Hinnant39213642013-07-23 22:01:58 +0000616#if _LIBCPP_DEBUG_LEVEL >= 2
617 _LIBCPP_INLINE_VISIBILITY
618 __hash_local_iterator(__node_pointer __node, size_t __bucket,
619 size_t __bucket_count, const void* __c) _NOEXCEPT
620 : __node_(__node),
621 __bucket_(__bucket),
622 __bucket_count_(__bucket_count)
623 {
624 __get_db()->__insert_ic(this, __c);
625 if (__node_ != nullptr)
626 __node_ = __node_->__next_;
627 }
628#else
Howard Hinnant99acc502010-09-21 17:32:39 +0000629 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000630 __hash_local_iterator(__node_pointer __node, size_t __bucket,
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000631 size_t __bucket_count) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000632 : __node_(__node),
633 __bucket_(__bucket),
634 __bucket_count_(__bucket_count)
635 {
636 if (__node_ != nullptr)
637 __node_ = __node_->__next_;
638 }
Howard Hinnant39213642013-07-23 22:01:58 +0000639#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000640 template <class, class, class, class> friend class __hash_table;
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000641 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_const_local_iterator;
642 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_map_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000643};
644
645template <class _ConstNodePtr>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000646class _LIBCPP_TYPE_VIS_ONLY __hash_const_local_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000647{
Eric Fiselier774c7c52016-02-10 20:46:23 +0000648 typedef __hash_node_types<_ConstNodePtr> _NodeTypes;
649 typedef _ConstNodePtr __node_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000650
651 __node_pointer __node_;
652 size_t __bucket_;
653 size_t __bucket_count_;
654
655 typedef pointer_traits<__node_pointer> __pointer_traits;
656 typedef typename __pointer_traits::element_type __node;
657 typedef typename remove_const<__node>::type __non_const_node;
Eric Fiselier5cf84e02015-12-30 21:52:00 +0000658 typedef typename __rebind_pointer<__node_pointer, __non_const_node>::type
659 __non_const_node_pointer;
Eric Fiselier0493d022016-02-18 00:20:34 +0000660public:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000661 typedef __hash_local_iterator<__non_const_node_pointer>
662 __non_const_iterator;
Eric Fiselier0493d022016-02-18 00:20:34 +0000663
Eric Fiselier774c7c52016-02-10 20:46:23 +0000664 typedef forward_iterator_tag iterator_category;
665 typedef typename _NodeTypes::__node_value_type value_type;
666 typedef typename _NodeTypes::difference_type difference_type;
667 typedef const value_type& reference;
668 typedef typename _NodeTypes::__const_node_value_type_pointer pointer;
Eric Fiselier5cf84e02015-12-30 21:52:00 +0000669
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000670
Howard Hinnant39213642013-07-23 22:01:58 +0000671 _LIBCPP_INLINE_VISIBILITY __hash_const_local_iterator() _NOEXCEPT
672 {
673#if _LIBCPP_DEBUG_LEVEL >= 2
674 __get_db()->__insert_i(this);
675#endif
676 }
677
Howard Hinnant99acc502010-09-21 17:32:39 +0000678 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000679 __hash_const_local_iterator(const __non_const_iterator& __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000680 : __node_(__x.__node_),
681 __bucket_(__x.__bucket_),
682 __bucket_count_(__x.__bucket_count_)
Howard Hinnant39213642013-07-23 22:01:58 +0000683 {
684#if _LIBCPP_DEBUG_LEVEL >= 2
685 __get_db()->__iterator_copy(this, &__x);
686#endif
687 }
688
689#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000690
Howard Hinnant99acc502010-09-21 17:32:39 +0000691 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000692 __hash_const_local_iterator(const __hash_const_local_iterator& __i)
693 : __node_(__i.__node_),
694 __bucket_(__i.__bucket_),
695 __bucket_count_(__i.__bucket_count_)
696 {
697 __get_db()->__iterator_copy(this, &__i);
698 }
699
Howard Hinnant99acc502010-09-21 17:32:39 +0000700 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000701 ~__hash_const_local_iterator()
702 {
703 __get_db()->__erase_i(this);
704 }
705
706 _LIBCPP_INLINE_VISIBILITY
707 __hash_const_local_iterator& operator=(const __hash_const_local_iterator& __i)
708 {
709 if (this != &__i)
710 {
711 __get_db()->__iterator_copy(this, &__i);
712 __node_ = __i.__node_;
713 __bucket_ = __i.__bucket_;
714 __bucket_count_ = __i.__bucket_count_;
715 }
716 return *this;
717 }
718
719#endif // _LIBCPP_DEBUG_LEVEL >= 2
720
721 _LIBCPP_INLINE_VISIBILITY
722 reference operator*() const
723 {
724#if _LIBCPP_DEBUG_LEVEL >= 2
725 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
726 "Attempted to dereference a non-dereferenceable unordered container const_local_iterator");
727#endif
728 return __node_->__value_;
729 }
730 _LIBCPP_INLINE_VISIBILITY
731 pointer operator->() const
732 {
733#if _LIBCPP_DEBUG_LEVEL >= 2
734 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
735 "Attempted to dereference a non-dereferenceable unordered container const_local_iterator");
736#endif
737 return pointer_traits<pointer>::pointer_to(__node_->__value_);
738 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000739
Howard Hinnant99acc502010-09-21 17:32:39 +0000740 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000741 __hash_const_local_iterator& operator++()
742 {
Howard Hinnant39213642013-07-23 22:01:58 +0000743#if _LIBCPP_DEBUG_LEVEL >= 2
744 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
745 "Attempted to increment non-incrementable unordered container const_local_iterator");
746#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000747 __node_ = __node_->__next_;
Howard Hinnant7a445152012-07-06 17:31:14 +0000748 if (__node_ != nullptr && __constrain_hash(__node_->__hash_, __bucket_count_) != __bucket_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000749 __node_ = nullptr;
750 return *this;
751 }
752
Howard Hinnant99acc502010-09-21 17:32:39 +0000753 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000754 __hash_const_local_iterator operator++(int)
755 {
756 __hash_const_local_iterator __t(*this);
757 ++(*this);
758 return __t;
759 }
760
Howard Hinnant99acc502010-09-21 17:32:39 +0000761 friend _LIBCPP_INLINE_VISIBILITY
762 bool operator==(const __hash_const_local_iterator& __x, const __hash_const_local_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58 +0000763 {
Howard Hinnant39213642013-07-23 22:01:58 +0000764 return __x.__node_ == __y.__node_;
765 }
Howard Hinnant99acc502010-09-21 17:32:39 +0000766 friend _LIBCPP_INLINE_VISIBILITY
767 bool operator!=(const __hash_const_local_iterator& __x, const __hash_const_local_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58 +0000768 {return !(__x == __y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000769
770private:
Howard Hinnant39213642013-07-23 22:01:58 +0000771#if _LIBCPP_DEBUG_LEVEL >= 2
772 _LIBCPP_INLINE_VISIBILITY
773 __hash_const_local_iterator(__node_pointer __node, size_t __bucket,
774 size_t __bucket_count, const void* __c) _NOEXCEPT
775 : __node_(__node),
776 __bucket_(__bucket),
777 __bucket_count_(__bucket_count)
778 {
779 __get_db()->__insert_ic(this, __c);
780 if (__node_ != nullptr)
781 __node_ = __node_->__next_;
782 }
783#else
Howard Hinnant99acc502010-09-21 17:32:39 +0000784 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000785 __hash_const_local_iterator(__node_pointer __node, size_t __bucket,
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000786 size_t __bucket_count) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000787 : __node_(__node),
788 __bucket_(__bucket),
789 __bucket_count_(__bucket_count)
790 {
791 if (__node_ != nullptr)
792 __node_ = __node_->__next_;
793 }
Howard Hinnant39213642013-07-23 22:01:58 +0000794#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000795 template <class, class, class, class> friend class __hash_table;
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000796 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_map_const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000797};
798
799template <class _Alloc>
800class __bucket_list_deallocator
801{
802 typedef _Alloc allocator_type;
803 typedef allocator_traits<allocator_type> __alloc_traits;
804 typedef typename __alloc_traits::size_type size_type;
805
806 __compressed_pair<size_type, allocator_type> __data_;
807public:
808 typedef typename __alloc_traits::pointer pointer;
809
Howard Hinnant99acc502010-09-21 17:32:39 +0000810 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000811 __bucket_list_deallocator()
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000812 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000813 : __data_(0) {}
Howard Hinnant99acc502010-09-21 17:32:39 +0000814
815 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000816 __bucket_list_deallocator(const allocator_type& __a, size_type __size)
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000817 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000818 : __data_(__size, __a) {}
819
Howard Hinnant73d21a42010-09-04 23:28:19 +0000820#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000821
Howard Hinnant99acc502010-09-21 17:32:39 +0000822 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000823 __bucket_list_deallocator(__bucket_list_deallocator&& __x)
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000824 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000825 : __data_(_VSTD::move(__x.__data_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000826 {
827 __x.size() = 0;
828 }
829
Howard Hinnant73d21a42010-09-04 23:28:19 +0000830#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000831
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000832 _LIBCPP_INLINE_VISIBILITY
833 size_type& size() _NOEXCEPT {return __data_.first();}
834 _LIBCPP_INLINE_VISIBILITY
835 size_type size() const _NOEXCEPT {return __data_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000836
Howard Hinnant99acc502010-09-21 17:32:39 +0000837 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000838 allocator_type& __alloc() _NOEXCEPT {return __data_.second();}
839 _LIBCPP_INLINE_VISIBILITY
840 const allocator_type& __alloc() const _NOEXCEPT {return __data_.second();}
841
842 _LIBCPP_INLINE_VISIBILITY
843 void operator()(pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000844 {
845 __alloc_traits::deallocate(__alloc(), __p, size());
846 }
847};
848
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000849template <class _Alloc> class __hash_map_node_destructor;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000850
851template <class _Alloc>
852class __hash_node_destructor
853{
854 typedef _Alloc allocator_type;
855 typedef allocator_traits<allocator_type> __alloc_traits;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000856
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000857public:
858 typedef typename __alloc_traits::pointer pointer;
859private:
Eric Fiselier2960ae22016-02-11 11:59:44 +0000860 typedef __hash_node_types<pointer> _NodeTypes;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000861
862 allocator_type& __na_;
863
864 __hash_node_destructor& operator=(const __hash_node_destructor&);
865
866public:
867 bool __value_constructed;
868
Howard Hinnant99acc502010-09-21 17:32:39 +0000869 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant199d0ae2011-07-31 17:04:30 +0000870 explicit __hash_node_destructor(allocator_type& __na,
871 bool __constructed = false) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000872 : __na_(__na),
Howard Hinnant199d0ae2011-07-31 17:04:30 +0000873 __value_constructed(__constructed)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000874 {}
875
Howard Hinnant99acc502010-09-21 17:32:39 +0000876 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000877 void operator()(pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000878 {
879 if (__value_constructed)
Eric Fiselier2960ae22016-02-11 11:59:44 +0000880 __alloc_traits::destroy(__na_, _NodeTypes::__get_ptr(__p->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000881 if (__p)
882 __alloc_traits::deallocate(__na_, __p, 1);
883 }
884
885 template <class> friend class __hash_map_node_destructor;
886};
887
888template <class _Tp, class _Hash, class _Equal, class _Alloc>
889class __hash_table
890{
891public:
892 typedef _Tp value_type;
893 typedef _Hash hasher;
894 typedef _Equal key_equal;
895 typedef _Alloc allocator_type;
896
897private:
898 typedef allocator_traits<allocator_type> __alloc_traits;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000899 typedef typename
900 __make_hash_node_types<value_type, typename __alloc_traits::void_pointer>::type
901 _NodeTypes;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000902public:
Eric Fiselier2960ae22016-02-11 11:59:44 +0000903
904 typedef typename _NodeTypes::__node_value_type __node_value_type;
905 typedef typename _NodeTypes::__container_value_type __container_value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000906 typedef value_type& reference;
907 typedef const value_type& const_reference;
908 typedef typename __alloc_traits::pointer pointer;
909 typedef typename __alloc_traits::const_pointer const_pointer;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000910#ifndef _LIBCPP_ABI_FIX_UNORDERED_CONTAINER_SIZE_TYPE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000911 typedef typename __alloc_traits::size_type size_type;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000912#else
913 typedef typename _NodeTypes::size_type size_type;
914#endif
915 typedef typename _NodeTypes::difference_type difference_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000916public:
917 // Create __node
Eric Fiselier774c7c52016-02-10 20:46:23 +0000918
919 typedef typename _NodeTypes::__node_type __node;
Marshall Clow66302c62015-04-07 05:21:38 +0000920 typedef typename __rebind_alloc_helper<__alloc_traits, __node>::type __node_allocator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000921 typedef allocator_traits<__node_allocator> __node_traits;
Eric Fiselier9e9f42e2016-02-11 15:22:37 +0000922 typedef typename _NodeTypes::__void_pointer __void_pointer;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000923 typedef typename _NodeTypes::__node_pointer __node_pointer;
924 typedef typename _NodeTypes::__node_pointer __node_const_pointer;
925 typedef typename _NodeTypes::__node_base_type __first_node;
926 typedef typename _NodeTypes::__node_base_pointer __node_base_pointer;
927
928private:
929 // check for sane allocator pointer rebinding semantics. Rebinding the
930 // allocator for a new pointer type should be exactly the same as rebinding
931 // the pointer using 'pointer_traits'.
932 static_assert((is_same<__node_pointer, typename __node_traits::pointer>::value),
933 "Allocator does not rebind pointers in a sane manner.");
934 typedef typename __rebind_alloc_helper<__node_traits, __first_node>::type
935 __node_base_allocator;
936 typedef allocator_traits<__node_base_allocator> __node_base_traits;
937 static_assert((is_same<__node_base_pointer, typename __node_base_traits::pointer>::value),
938 "Allocator does not rebind pointers in a sane manner.");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000939
940private:
941
Marshall Clow66302c62015-04-07 05:21:38 +0000942 typedef typename __rebind_alloc_helper<__node_traits, __node_pointer>::type __pointer_allocator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000943 typedef __bucket_list_deallocator<__pointer_allocator> __bucket_list_deleter;
944 typedef unique_ptr<__node_pointer[], __bucket_list_deleter> __bucket_list;
945 typedef allocator_traits<__pointer_allocator> __pointer_alloc_traits;
946 typedef typename __bucket_list_deleter::pointer __node_pointer_pointer;
947
948 // --- Member data begin ---
Eric Fiselier774c7c52016-02-10 20:46:23 +0000949 __bucket_list __bucket_list_;
950 __compressed_pair<__first_node, __node_allocator> __p1_;
951 __compressed_pair<size_type, hasher> __p2_;
952 __compressed_pair<float, key_equal> __p3_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000953 // --- Member data end ---
954
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000955 _LIBCPP_INLINE_VISIBILITY
956 size_type& size() _NOEXCEPT {return __p2_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000957public:
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000958 _LIBCPP_INLINE_VISIBILITY
959 size_type size() const _NOEXCEPT {return __p2_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000960
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000961 _LIBCPP_INLINE_VISIBILITY
962 hasher& hash_function() _NOEXCEPT {return __p2_.second();}
963 _LIBCPP_INLINE_VISIBILITY
964 const hasher& hash_function() const _NOEXCEPT {return __p2_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000965
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000966 _LIBCPP_INLINE_VISIBILITY
967 float& max_load_factor() _NOEXCEPT {return __p3_.first();}
968 _LIBCPP_INLINE_VISIBILITY
969 float max_load_factor() const _NOEXCEPT {return __p3_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000970
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000971 _LIBCPP_INLINE_VISIBILITY
972 key_equal& key_eq() _NOEXCEPT {return __p3_.second();}
973 _LIBCPP_INLINE_VISIBILITY
974 const key_equal& key_eq() const _NOEXCEPT {return __p3_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000975
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000976 _LIBCPP_INLINE_VISIBILITY
977 __node_allocator& __node_alloc() _NOEXCEPT {return __p1_.second();}
978 _LIBCPP_INLINE_VISIBILITY
979 const __node_allocator& __node_alloc() const _NOEXCEPT
980 {return __p1_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000981
982public:
983 typedef __hash_iterator<__node_pointer> iterator;
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +0000984 typedef __hash_const_iterator<__node_pointer> const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000985 typedef __hash_local_iterator<__node_pointer> local_iterator;
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +0000986 typedef __hash_const_local_iterator<__node_pointer> const_local_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000987
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000988 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000989 __hash_table()
990 _NOEXCEPT_(
991 is_nothrow_default_constructible<__bucket_list>::value &&
992 is_nothrow_default_constructible<__first_node>::value &&
993 is_nothrow_default_constructible<__node_allocator>::value &&
994 is_nothrow_default_constructible<hasher>::value &&
995 is_nothrow_default_constructible<key_equal>::value);
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000996 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000997 __hash_table(const hasher& __hf, const key_equal& __eql);
998 __hash_table(const hasher& __hf, const key_equal& __eql,
999 const allocator_type& __a);
1000 explicit __hash_table(const allocator_type& __a);
1001 __hash_table(const __hash_table& __u);
1002 __hash_table(const __hash_table& __u, const allocator_type& __a);
Eric Fiselier2960ae22016-02-11 11:59:44 +00001003#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001004 __hash_table(__hash_table&& __u)
1005 _NOEXCEPT_(
1006 is_nothrow_move_constructible<__bucket_list>::value &&
1007 is_nothrow_move_constructible<__first_node>::value &&
1008 is_nothrow_move_constructible<__node_allocator>::value &&
1009 is_nothrow_move_constructible<hasher>::value &&
1010 is_nothrow_move_constructible<key_equal>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001011 __hash_table(__hash_table&& __u, const allocator_type& __a);
Eric Fiselier2960ae22016-02-11 11:59:44 +00001012#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001013 ~__hash_table();
1014
1015 __hash_table& operator=(const __hash_table& __u);
Eric Fiselier2960ae22016-02-11 11:59:44 +00001016#ifndef _LIBCPP_CXX03_LANG
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001017 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001018 __hash_table& operator=(__hash_table&& __u)
1019 _NOEXCEPT_(
1020 __node_traits::propagate_on_container_move_assignment::value &&
1021 is_nothrow_move_assignable<__node_allocator>::value &&
1022 is_nothrow_move_assignable<hasher>::value &&
1023 is_nothrow_move_assignable<key_equal>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001024#endif
1025 template <class _InputIterator>
1026 void __assign_unique(_InputIterator __first, _InputIterator __last);
1027 template <class _InputIterator>
1028 void __assign_multi(_InputIterator __first, _InputIterator __last);
1029
Howard Hinnant99acc502010-09-21 17:32:39 +00001030 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001031 size_type max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001032 {
1033 return allocator_traits<__pointer_allocator>::max_size(
1034 __bucket_list_.get_deleter().__alloc());
1035 }
1036
1037 pair<iterator, bool> __node_insert_unique(__node_pointer __nd);
1038 iterator __node_insert_multi(__node_pointer __nd);
1039 iterator __node_insert_multi(const_iterator __p,
1040 __node_pointer __nd);
1041
Eric Fiselier2960ae22016-02-11 11:59:44 +00001042#ifndef _LIBCPP_CXX03_LANG
1043 template <class _Key, class ..._Args>
1044 pair<iterator, bool> __emplace_unique_key_args(_Key const& __k, _Args&&... __args);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001045
Eric Fiselier2960ae22016-02-11 11:59:44 +00001046 template <class... _Args>
1047 pair<iterator, bool> __emplace_unique(_Args&&... __args);
1048 template <class... _Args>
1049 iterator __emplace_multi(_Args&&... __args);
1050 template <class... _Args>
1051 iterator __emplace_hint_multi(const_iterator __p, _Args&&... __args);
1052
1053
Eric Fiselierfdae69a2015-06-13 07:18:32 +00001054 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2960ae22016-02-11 11:59:44 +00001055 pair<iterator, bool>
1056 __insert_unique(__container_value_type&& __x) {
1057 return __emplace_unique_key_args(_NodeTypes::__get_key(__x), _VSTD::move(__x));
1058 }
1059
1060 template <class _Pp, class = typename enable_if<
1061 !__is_same_uncvref<_Pp, __container_value_type>::value
1062 >::type>
Eric Fiselierfdae69a2015-06-13 07:18:32 +00001063 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2960ae22016-02-11 11:59:44 +00001064 pair<iterator, bool> __insert_unique(_Pp&& __x) {
1065 return __emplace_unique(_VSTD::forward<_Pp>(__x));
1066 }
1067
1068 template <class _Pp>
1069 _LIBCPP_INLINE_VISIBILITY
1070 iterator __insert_multi(_Pp&& __x) {
1071 return __emplace_multi(_VSTD::forward<_Pp>(__x));
1072 }
1073
1074 template <class _Pp>
1075 _LIBCPP_INLINE_VISIBILITY
1076 iterator __insert_multi(const_iterator __p, _Pp&& __x) {
1077 return __emplace_hint_multi(__p, _VSTD::forward<_Pp>(__x));
1078 }
1079
1080#else // !defined(_LIBCPP_CXX03_LANG)
1081 template <class _Key, class _Args>
1082 pair<iterator, bool> __emplace_unique_key_args(_Key const&, _Args& __args);
1083
1084 iterator __insert_multi(const __container_value_type& __x);
1085 iterator __insert_multi(const_iterator __p, const __container_value_type& __x);
Eric Fiselierfdae69a2015-06-13 07:18:32 +00001086#endif
1087
Eric Fiselier2960ae22016-02-11 11:59:44 +00001088 _LIBCPP_INLINE_VISIBILITY
1089 pair<iterator, bool> __insert_unique(const __container_value_type& __x) {
1090 return __emplace_unique_key_args(_NodeTypes::__get_key(__x), __x);
1091 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001092
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001093 void clear() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001094 void rehash(size_type __n);
Howard Hinnant99acc502010-09-21 17:32:39 +00001095 _LIBCPP_INLINE_VISIBILITY void reserve(size_type __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001096 {rehash(static_cast<size_type>(ceil(__n / max_load_factor())));}
Howard Hinnant99acc502010-09-21 17:32:39 +00001097
1098 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001099 size_type bucket_count() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001100 {
1101 return __bucket_list_.get_deleter().size();
1102 }
1103
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001104 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001105 iterator begin() _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001106 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001107 iterator end() _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001108 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001109 const_iterator begin() const _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001110 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001111 const_iterator end() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001112
1113 template <class _Key>
Howard Hinnant99acc502010-09-21 17:32:39 +00001114 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001115 size_type bucket(const _Key& __k) const
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00001116 {
1117 _LIBCPP_ASSERT(bucket_count() > 0,
1118 "unordered container::bucket(key) called when bucket_count() == 0");
1119 return __constrain_hash(hash_function()(__k), bucket_count());
1120 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001121
1122 template <class _Key>
1123 iterator find(const _Key& __x);
1124 template <class _Key>
1125 const_iterator find(const _Key& __x) const;
1126
Howard Hinnant99968442011-11-29 18:15:50 +00001127 typedef __hash_node_destructor<__node_allocator> _Dp;
1128 typedef unique_ptr<__node, _Dp> __node_holder;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001129
1130 iterator erase(const_iterator __p);
1131 iterator erase(const_iterator __first, const_iterator __last);
1132 template <class _Key>
1133 size_type __erase_unique(const _Key& __k);
1134 template <class _Key>
1135 size_type __erase_multi(const _Key& __k);
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001136 __node_holder remove(const_iterator __p) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001137
1138 template <class _Key>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001139 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001140 size_type __count_unique(const _Key& __k) const;
1141 template <class _Key>
1142 size_type __count_multi(const _Key& __k) const;
1143
1144 template <class _Key>
1145 pair<iterator, iterator>
1146 __equal_range_unique(const _Key& __k);
1147 template <class _Key>
1148 pair<const_iterator, const_iterator>
1149 __equal_range_unique(const _Key& __k) const;
1150
1151 template <class _Key>
1152 pair<iterator, iterator>
1153 __equal_range_multi(const _Key& __k);
1154 template <class _Key>
1155 pair<const_iterator, const_iterator>
1156 __equal_range_multi(const _Key& __k) const;
1157
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001158 void swap(__hash_table& __u)
Eric Fiselier692177d2015-07-18 20:40:46 +00001159#if _LIBCPP_STD_VER <= 11
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001160 _NOEXCEPT_(
Marshall Clow7d914d12015-07-13 20:04:56 +00001161 __is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value
Marshall Clow7d914d12015-07-13 20:04:56 +00001162 && (!allocator_traits<__pointer_allocator>::propagate_on_container_swap::value
1163 || __is_nothrow_swappable<__pointer_allocator>::value)
1164 && (!__node_traits::propagate_on_container_swap::value
1165 || __is_nothrow_swappable<__node_allocator>::value)
Marshall Clow7d914d12015-07-13 20:04:56 +00001166 );
Eric Fiselier692177d2015-07-18 20:40:46 +00001167#else
1168 _NOEXCEPT_(__is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value);
1169#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001170
Howard Hinnant99acc502010-09-21 17:32:39 +00001171 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001172 size_type max_bucket_count() const _NOEXCEPT
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001173 {return __pointer_alloc_traits::max_size(__bucket_list_.get_deleter().__alloc());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001174 size_type bucket_size(size_type __n) const;
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001175 _LIBCPP_INLINE_VISIBILITY float load_factor() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001176 {
1177 size_type __bc = bucket_count();
1178 return __bc != 0 ? (float)size() / __bc : 0.f;
1179 }
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001180 _LIBCPP_INLINE_VISIBILITY void max_load_factor(float __mlf) _NOEXCEPT
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00001181 {
1182 _LIBCPP_ASSERT(__mlf > 0,
1183 "unordered container::max_load_factor(lf) called with lf <= 0");
1184 max_load_factor() = _VSTD::max(__mlf, load_factor());
1185 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001186
Howard Hinnant39213642013-07-23 22:01:58 +00001187 _LIBCPP_INLINE_VISIBILITY
1188 local_iterator
1189 begin(size_type __n)
1190 {
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00001191 _LIBCPP_ASSERT(__n < bucket_count(),
1192 "unordered container::begin(n) called with n >= bucket_count()");
Howard Hinnant39213642013-07-23 22:01:58 +00001193#if _LIBCPP_DEBUG_LEVEL >= 2
1194 return local_iterator(__bucket_list_[__n], __n, bucket_count(), this);
1195#else
1196 return local_iterator(__bucket_list_[__n], __n, bucket_count());
1197#endif
1198 }
1199
1200 _LIBCPP_INLINE_VISIBILITY
1201 local_iterator
1202 end(size_type __n)
1203 {
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00001204 _LIBCPP_ASSERT(__n < bucket_count(),
1205 "unordered container::end(n) called with n >= bucket_count()");
Howard Hinnant39213642013-07-23 22:01:58 +00001206#if _LIBCPP_DEBUG_LEVEL >= 2
1207 return local_iterator(nullptr, __n, bucket_count(), this);
1208#else
1209 return local_iterator(nullptr, __n, bucket_count());
1210#endif
1211 }
1212
1213 _LIBCPP_INLINE_VISIBILITY
1214 const_local_iterator
1215 cbegin(size_type __n) const
1216 {
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00001217 _LIBCPP_ASSERT(__n < bucket_count(),
1218 "unordered container::cbegin(n) called with n >= bucket_count()");
Howard Hinnant39213642013-07-23 22:01:58 +00001219#if _LIBCPP_DEBUG_LEVEL >= 2
1220 return const_local_iterator(__bucket_list_[__n], __n, bucket_count(), this);
1221#else
1222 return const_local_iterator(__bucket_list_[__n], __n, bucket_count());
1223#endif
1224 }
1225
1226 _LIBCPP_INLINE_VISIBILITY
1227 const_local_iterator
1228 cend(size_type __n) const
1229 {
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00001230 _LIBCPP_ASSERT(__n < bucket_count(),
1231 "unordered container::cend(n) called with n >= bucket_count()");
Howard Hinnant39213642013-07-23 22:01:58 +00001232#if _LIBCPP_DEBUG_LEVEL >= 2
1233 return const_local_iterator(nullptr, __n, bucket_count(), this);
1234#else
1235 return const_local_iterator(nullptr, __n, bucket_count());
1236#endif
1237 }
1238
1239#if _LIBCPP_DEBUG_LEVEL >= 2
1240
1241 bool __dereferenceable(const const_iterator* __i) const;
1242 bool __decrementable(const const_iterator* __i) const;
1243 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1244 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1245
1246#endif // _LIBCPP_DEBUG_LEVEL >= 2
1247
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001248private:
1249 void __rehash(size_type __n);
1250
Eric Fiselier2960ae22016-02-11 11:59:44 +00001251#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001252 template <class ..._Args>
Eric Fiselier2960ae22016-02-11 11:59:44 +00001253 __node_holder __construct_node(_Args&& ...__args);
1254
1255 template <class _First, class ..._Rest>
1256 __node_holder __construct_node_hash(size_t __hash, _First&& __f, _Rest&&... __rest);
1257#else // _LIBCPP_CXX03_LANG
1258 __node_holder __construct_node(const __container_value_type& __v);
1259 __node_holder __construct_node_hash(size_t __hash, const __container_value_type& __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001260#endif
Eric Fiselier2960ae22016-02-11 11:59:44 +00001261
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001262
Howard Hinnant99acc502010-09-21 17:32:39 +00001263 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001264 void __copy_assign_alloc(const __hash_table& __u)
1265 {__copy_assign_alloc(__u, integral_constant<bool,
1266 __node_traits::propagate_on_container_copy_assignment::value>());}
1267 void __copy_assign_alloc(const __hash_table& __u, true_type);
Howard Hinnant99acc502010-09-21 17:32:39 +00001268 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00001269 void __copy_assign_alloc(const __hash_table&, false_type) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001270
Eric Fiselier2960ae22016-02-11 11:59:44 +00001271#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001272 void __move_assign(__hash_table& __u, false_type);
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001273 void __move_assign(__hash_table& __u, true_type)
1274 _NOEXCEPT_(
1275 is_nothrow_move_assignable<__node_allocator>::value &&
1276 is_nothrow_move_assignable<hasher>::value &&
1277 is_nothrow_move_assignable<key_equal>::value);
1278 _LIBCPP_INLINE_VISIBILITY
1279 void __move_assign_alloc(__hash_table& __u)
1280 _NOEXCEPT_(
1281 !__node_traits::propagate_on_container_move_assignment::value ||
1282 (is_nothrow_move_assignable<__pointer_allocator>::value &&
1283 is_nothrow_move_assignable<__node_allocator>::value))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001284 {__move_assign_alloc(__u, integral_constant<bool,
1285 __node_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnant99acc502010-09-21 17:32:39 +00001286 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001287 void __move_assign_alloc(__hash_table& __u, true_type)
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001288 _NOEXCEPT_(
1289 is_nothrow_move_assignable<__pointer_allocator>::value &&
1290 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001291 {
1292 __bucket_list_.get_deleter().__alloc() =
Howard Hinnant0949eed2011-06-30 21:18:19 +00001293 _VSTD::move(__u.__bucket_list_.get_deleter().__alloc());
1294 __node_alloc() = _VSTD::move(__u.__node_alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001295 }
Howard Hinnant99acc502010-09-21 17:32:39 +00001296 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001297 void __move_assign_alloc(__hash_table&, false_type) _NOEXCEPT {}
Eric Fiselier2960ae22016-02-11 11:59:44 +00001298#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001299
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001300 void __deallocate(__node_pointer __np) _NOEXCEPT;
1301 __node_pointer __detach() _NOEXCEPT;
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001302
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001303 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_map;
1304 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_multimap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001305};
1306
1307template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001308inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001309__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table()
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001310 _NOEXCEPT_(
1311 is_nothrow_default_constructible<__bucket_list>::value &&
1312 is_nothrow_default_constructible<__first_node>::value &&
Eric Fiselierc8f54c22015-12-16 00:53:04 +00001313 is_nothrow_default_constructible<__node_allocator>::value &&
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001314 is_nothrow_default_constructible<hasher>::value &&
1315 is_nothrow_default_constructible<key_equal>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001316 : __p2_(0),
1317 __p3_(1.0f)
1318{
1319}
1320
1321template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001322inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001323__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const hasher& __hf,
1324 const key_equal& __eql)
1325 : __bucket_list_(nullptr, __bucket_list_deleter()),
1326 __p1_(),
1327 __p2_(0, __hf),
1328 __p3_(1.0f, __eql)
1329{
1330}
1331
1332template <class _Tp, class _Hash, class _Equal, class _Alloc>
1333__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const hasher& __hf,
1334 const key_equal& __eql,
1335 const allocator_type& __a)
1336 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1337 __p1_(__node_allocator(__a)),
1338 __p2_(0, __hf),
1339 __p3_(1.0f, __eql)
1340{
1341}
1342
1343template <class _Tp, class _Hash, class _Equal, class _Alloc>
1344__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const allocator_type& __a)
1345 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1346 __p1_(__node_allocator(__a)),
1347 __p2_(0),
1348 __p3_(1.0f)
1349{
1350}
1351
1352template <class _Tp, class _Hash, class _Equal, class _Alloc>
1353__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const __hash_table& __u)
1354 : __bucket_list_(nullptr,
1355 __bucket_list_deleter(allocator_traits<__pointer_allocator>::
1356 select_on_container_copy_construction(
1357 __u.__bucket_list_.get_deleter().__alloc()), 0)),
1358 __p1_(allocator_traits<__node_allocator>::
1359 select_on_container_copy_construction(__u.__node_alloc())),
1360 __p2_(0, __u.hash_function()),
1361 __p3_(__u.__p3_)
1362{
1363}
1364
1365template <class _Tp, class _Hash, class _Equal, class _Alloc>
1366__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const __hash_table& __u,
1367 const allocator_type& __a)
1368 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1369 __p1_(__node_allocator(__a)),
1370 __p2_(0, __u.hash_function()),
1371 __p3_(__u.__p3_)
1372{
1373}
1374
Eric Fiselier2960ae22016-02-11 11:59:44 +00001375#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001376
1377template <class _Tp, class _Hash, class _Equal, class _Alloc>
1378__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(__hash_table&& __u)
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001379 _NOEXCEPT_(
1380 is_nothrow_move_constructible<__bucket_list>::value &&
1381 is_nothrow_move_constructible<__first_node>::value &&
Eric Fiselierc8f54c22015-12-16 00:53:04 +00001382 is_nothrow_move_constructible<__node_allocator>::value &&
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001383 is_nothrow_move_constructible<hasher>::value &&
1384 is_nothrow_move_constructible<key_equal>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001385 : __bucket_list_(_VSTD::move(__u.__bucket_list_)),
1386 __p1_(_VSTD::move(__u.__p1_)),
1387 __p2_(_VSTD::move(__u.__p2_)),
1388 __p3_(_VSTD::move(__u.__p3_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001389{
1390 if (size() > 0)
1391 {
Howard Hinnant7a445152012-07-06 17:31:14 +00001392 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash_, bucket_count())] =
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001393 static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001394 __u.__p1_.first().__next_ = nullptr;
1395 __u.size() = 0;
1396 }
1397}
1398
1399template <class _Tp, class _Hash, class _Equal, class _Alloc>
1400__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(__hash_table&& __u,
1401 const allocator_type& __a)
1402 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1403 __p1_(__node_allocator(__a)),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001404 __p2_(0, _VSTD::move(__u.hash_function())),
1405 __p3_(_VSTD::move(__u.__p3_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001406{
1407 if (__a == allocator_type(__u.__node_alloc()))
1408 {
1409 __bucket_list_.reset(__u.__bucket_list_.release());
1410 __bucket_list_.get_deleter().size() = __u.__bucket_list_.get_deleter().size();
1411 __u.__bucket_list_.get_deleter().size() = 0;
1412 if (__u.size() > 0)
1413 {
1414 __p1_.first().__next_ = __u.__p1_.first().__next_;
1415 __u.__p1_.first().__next_ = nullptr;
Howard Hinnant7a445152012-07-06 17:31:14 +00001416 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash_, bucket_count())] =
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001417 static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001418 size() = __u.size();
1419 __u.size() = 0;
1420 }
1421 }
1422}
1423
Eric Fiselier2960ae22016-02-11 11:59:44 +00001424#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001425
1426template <class _Tp, class _Hash, class _Equal, class _Alloc>
1427__hash_table<_Tp, _Hash, _Equal, _Alloc>::~__hash_table()
1428{
1429 __deallocate(__p1_.first().__next_);
Howard Hinnant39213642013-07-23 22:01:58 +00001430#if _LIBCPP_DEBUG_LEVEL >= 2
1431 __get_db()->__erase_c(this);
1432#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001433}
1434
1435template <class _Tp, class _Hash, class _Equal, class _Alloc>
1436void
1437__hash_table<_Tp, _Hash, _Equal, _Alloc>::__copy_assign_alloc(
1438 const __hash_table& __u, true_type)
1439{
1440 if (__node_alloc() != __u.__node_alloc())
1441 {
1442 clear();
1443 __bucket_list_.reset();
1444 __bucket_list_.get_deleter().size() = 0;
1445 }
1446 __bucket_list_.get_deleter().__alloc() = __u.__bucket_list_.get_deleter().__alloc();
1447 __node_alloc() = __u.__node_alloc();
1448}
1449
1450template <class _Tp, class _Hash, class _Equal, class _Alloc>
1451__hash_table<_Tp, _Hash, _Equal, _Alloc>&
1452__hash_table<_Tp, _Hash, _Equal, _Alloc>::operator=(const __hash_table& __u)
1453{
1454 if (this != &__u)
1455 {
1456 __copy_assign_alloc(__u);
1457 hash_function() = __u.hash_function();
1458 key_eq() = __u.key_eq();
1459 max_load_factor() = __u.max_load_factor();
1460 __assign_multi(__u.begin(), __u.end());
1461 }
1462 return *this;
1463}
1464
1465template <class _Tp, class _Hash, class _Equal, class _Alloc>
1466void
1467__hash_table<_Tp, _Hash, _Equal, _Alloc>::__deallocate(__node_pointer __np)
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001468 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001469{
1470 __node_allocator& __na = __node_alloc();
1471 while (__np != nullptr)
1472 {
1473 __node_pointer __next = __np->__next_;
Howard Hinnant39213642013-07-23 22:01:58 +00001474#if _LIBCPP_DEBUG_LEVEL >= 2
1475 __c_node* __c = __get_db()->__find_c_and_lock(this);
1476 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1477 {
1478 --__p;
1479 iterator* __i = static_cast<iterator*>((*__p)->__i_);
1480 if (__i->__node_ == __np)
1481 {
1482 (*__p)->__c_ = nullptr;
1483 if (--__c->end_ != __p)
1484 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1485 }
1486 }
1487 __get_db()->unlock();
1488#endif
Eric Fiselier2960ae22016-02-11 11:59:44 +00001489 __node_traits::destroy(__na, _NodeTypes::__get_ptr(__np->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001490 __node_traits::deallocate(__na, __np, 1);
1491 __np = __next;
1492 }
1493}
1494
1495template <class _Tp, class _Hash, class _Equal, class _Alloc>
1496typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_pointer
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001497__hash_table<_Tp, _Hash, _Equal, _Alloc>::__detach() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001498{
1499 size_type __bc = bucket_count();
1500 for (size_type __i = 0; __i < __bc; ++__i)
1501 __bucket_list_[__i] = nullptr;
1502 size() = 0;
1503 __node_pointer __cache = __p1_.first().__next_;
1504 __p1_.first().__next_ = nullptr;
1505 return __cache;
1506}
1507
Eric Fiselier2960ae22016-02-11 11:59:44 +00001508#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001509
1510template <class _Tp, class _Hash, class _Equal, class _Alloc>
1511void
1512__hash_table<_Tp, _Hash, _Equal, _Alloc>::__move_assign(
1513 __hash_table& __u, true_type)
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001514 _NOEXCEPT_(
1515 is_nothrow_move_assignable<__node_allocator>::value &&
1516 is_nothrow_move_assignable<hasher>::value &&
1517 is_nothrow_move_assignable<key_equal>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001518{
1519 clear();
1520 __bucket_list_.reset(__u.__bucket_list_.release());
1521 __bucket_list_.get_deleter().size() = __u.__bucket_list_.get_deleter().size();
1522 __u.__bucket_list_.get_deleter().size() = 0;
1523 __move_assign_alloc(__u);
1524 size() = __u.size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001525 hash_function() = _VSTD::move(__u.hash_function());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001526 max_load_factor() = __u.max_load_factor();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001527 key_eq() = _VSTD::move(__u.key_eq());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001528 __p1_.first().__next_ = __u.__p1_.first().__next_;
1529 if (size() > 0)
1530 {
Howard Hinnant7a445152012-07-06 17:31:14 +00001531 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash_, bucket_count())] =
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001532 static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001533 __u.__p1_.first().__next_ = nullptr;
1534 __u.size() = 0;
1535 }
Howard Hinnant39213642013-07-23 22:01:58 +00001536#if _LIBCPP_DEBUG_LEVEL >= 2
1537 __get_db()->swap(this, &__u);
1538#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001539}
1540
1541template <class _Tp, class _Hash, class _Equal, class _Alloc>
1542void
1543__hash_table<_Tp, _Hash, _Equal, _Alloc>::__move_assign(
1544 __hash_table& __u, false_type)
1545{
1546 if (__node_alloc() == __u.__node_alloc())
1547 __move_assign(__u, true_type());
1548 else
1549 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001550 hash_function() = _VSTD::move(__u.hash_function());
1551 key_eq() = _VSTD::move(__u.key_eq());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001552 max_load_factor() = __u.max_load_factor();
1553 if (bucket_count() != 0)
1554 {
1555 __node_pointer __cache = __detach();
1556#ifndef _LIBCPP_NO_EXCEPTIONS
1557 try
1558 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001559#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001560 const_iterator __i = __u.begin();
1561 while (__cache != nullptr && __u.size() != 0)
1562 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001563 __cache->__value_ = _VSTD::move(__u.remove(__i++)->__value_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001564 __node_pointer __next = __cache->__next_;
1565 __node_insert_multi(__cache);
1566 __cache = __next;
1567 }
1568#ifndef _LIBCPP_NO_EXCEPTIONS
1569 }
1570 catch (...)
1571 {
1572 __deallocate(__cache);
1573 throw;
1574 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001575#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001576 __deallocate(__cache);
1577 }
1578 const_iterator __i = __u.begin();
1579 while (__u.size() != 0)
1580 {
Eric Fiselier2960ae22016-02-11 11:59:44 +00001581 __node_holder __h = __construct_node(_NodeTypes::__move(__u.remove(__i++)->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001582 __node_insert_multi(__h.get());
1583 __h.release();
1584 }
1585 }
1586}
1587
1588template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001589inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001590__hash_table<_Tp, _Hash, _Equal, _Alloc>&
1591__hash_table<_Tp, _Hash, _Equal, _Alloc>::operator=(__hash_table&& __u)
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001592 _NOEXCEPT_(
1593 __node_traits::propagate_on_container_move_assignment::value &&
1594 is_nothrow_move_assignable<__node_allocator>::value &&
1595 is_nothrow_move_assignable<hasher>::value &&
1596 is_nothrow_move_assignable<key_equal>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001597{
1598 __move_assign(__u, integral_constant<bool,
1599 __node_traits::propagate_on_container_move_assignment::value>());
1600 return *this;
1601}
1602
Eric Fiselier2960ae22016-02-11 11:59:44 +00001603#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001604
1605template <class _Tp, class _Hash, class _Equal, class _Alloc>
1606template <class _InputIterator>
1607void
1608__hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_unique(_InputIterator __first,
1609 _InputIterator __last)
1610{
Eric Fiselier2960ae22016-02-11 11:59:44 +00001611 typedef iterator_traits<_InputIterator> _ITraits;
1612 typedef typename _ITraits::value_type _ItValueType;
1613 static_assert((is_same<_ItValueType, __container_value_type>::value),
1614 "__assign_unique may only be called with the containers value type");
1615
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001616 if (bucket_count() != 0)
1617 {
1618 __node_pointer __cache = __detach();
1619#ifndef _LIBCPP_NO_EXCEPTIONS
1620 try
1621 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001622#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001623 for (; __cache != nullptr && __first != __last; ++__first)
1624 {
1625 __cache->__value_ = *__first;
1626 __node_pointer __next = __cache->__next_;
1627 __node_insert_unique(__cache);
1628 __cache = __next;
1629 }
1630#ifndef _LIBCPP_NO_EXCEPTIONS
1631 }
1632 catch (...)
1633 {
1634 __deallocate(__cache);
1635 throw;
1636 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001637#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001638 __deallocate(__cache);
1639 }
1640 for (; __first != __last; ++__first)
1641 __insert_unique(*__first);
1642}
1643
1644template <class _Tp, class _Hash, class _Equal, class _Alloc>
1645template <class _InputIterator>
1646void
1647__hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_multi(_InputIterator __first,
1648 _InputIterator __last)
1649{
Eric Fiselier2960ae22016-02-11 11:59:44 +00001650 typedef iterator_traits<_InputIterator> _ITraits;
1651 typedef typename _ITraits::value_type _ItValueType;
1652 static_assert((is_same<_ItValueType, __container_value_type>::value ||
1653 is_same<_ItValueType, __node_value_type>::value),
1654 "__assign_multi may only be called with the containers value type"
1655 " or the nodes value type");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001656 if (bucket_count() != 0)
1657 {
1658 __node_pointer __cache = __detach();
1659#ifndef _LIBCPP_NO_EXCEPTIONS
1660 try
1661 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001662#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001663 for (; __cache != nullptr && __first != __last; ++__first)
1664 {
1665 __cache->__value_ = *__first;
1666 __node_pointer __next = __cache->__next_;
1667 __node_insert_multi(__cache);
1668 __cache = __next;
1669 }
1670#ifndef _LIBCPP_NO_EXCEPTIONS
1671 }
1672 catch (...)
1673 {
1674 __deallocate(__cache);
1675 throw;
1676 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001677#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001678 __deallocate(__cache);
1679 }
1680 for (; __first != __last; ++__first)
Eric Fiselier2960ae22016-02-11 11:59:44 +00001681 __insert_multi(_NodeTypes::__get_value(*__first));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001682}
1683
1684template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001685inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001686typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001687__hash_table<_Tp, _Hash, _Equal, _Alloc>::begin() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001688{
Howard Hinnant39213642013-07-23 22:01:58 +00001689#if _LIBCPP_DEBUG_LEVEL >= 2
1690 return iterator(__p1_.first().__next_, this);
1691#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001692 return iterator(__p1_.first().__next_);
Howard Hinnant39213642013-07-23 22:01:58 +00001693#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001694}
1695
1696template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001697inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001698typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001699__hash_table<_Tp, _Hash, _Equal, _Alloc>::end() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001700{
Howard Hinnant39213642013-07-23 22:01:58 +00001701#if _LIBCPP_DEBUG_LEVEL >= 2
1702 return iterator(nullptr, this);
1703#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001704 return iterator(nullptr);
Howard Hinnant39213642013-07-23 22:01:58 +00001705#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001706}
1707
1708template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001709inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001710typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001711__hash_table<_Tp, _Hash, _Equal, _Alloc>::begin() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001712{
Howard Hinnant39213642013-07-23 22:01:58 +00001713#if _LIBCPP_DEBUG_LEVEL >= 2
1714 return const_iterator(__p1_.first().__next_, this);
1715#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001716 return const_iterator(__p1_.first().__next_);
Howard Hinnant39213642013-07-23 22:01:58 +00001717#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001718}
1719
1720template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001721inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001722typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001723__hash_table<_Tp, _Hash, _Equal, _Alloc>::end() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001724{
Howard Hinnant39213642013-07-23 22:01:58 +00001725#if _LIBCPP_DEBUG_LEVEL >= 2
1726 return const_iterator(nullptr, this);
1727#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001728 return const_iterator(nullptr);
Howard Hinnant39213642013-07-23 22:01:58 +00001729#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001730}
1731
1732template <class _Tp, class _Hash, class _Equal, class _Alloc>
1733void
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001734__hash_table<_Tp, _Hash, _Equal, _Alloc>::clear() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001735{
1736 if (size() > 0)
1737 {
1738 __deallocate(__p1_.first().__next_);
1739 __p1_.first().__next_ = nullptr;
1740 size_type __bc = bucket_count();
Howard Hinnant9f66bff2011-07-05 14:14:17 +00001741 for (size_type __i = 0; __i < __bc; ++__i)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001742 __bucket_list_[__i] = nullptr;
1743 size() = 0;
1744 }
1745}
1746
1747template <class _Tp, class _Hash, class _Equal, class _Alloc>
1748pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
1749__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_unique(__node_pointer __nd)
1750{
1751 __nd->__hash_ = hash_function()(__nd->__value_);
1752 size_type __bc = bucket_count();
1753 bool __inserted = false;
1754 __node_pointer __ndptr;
1755 size_t __chash;
1756 if (__bc != 0)
1757 {
Howard Hinnant7a445152012-07-06 17:31:14 +00001758 __chash = __constrain_hash(__nd->__hash_, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001759 __ndptr = __bucket_list_[__chash];
1760 if (__ndptr != nullptr)
1761 {
1762 for (__ndptr = __ndptr->__next_; __ndptr != nullptr &&
Howard Hinnant7a445152012-07-06 17:31:14 +00001763 __constrain_hash(__ndptr->__hash_, __bc) == __chash;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001764 __ndptr = __ndptr->__next_)
1765 {
1766 if (key_eq()(__ndptr->__value_, __nd->__value_))
1767 goto __done;
1768 }
1769 }
1770 }
1771 {
1772 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1773 {
Eric Fiselier57947ca2015-02-02 21:31:48 +00001774 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001775 size_type(ceil(float(size() + 1) / max_load_factor()))));
1776 __bc = bucket_count();
Howard Hinnant7a445152012-07-06 17:31:14 +00001777 __chash = __constrain_hash(__nd->__hash_, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001778 }
1779 // insert_after __bucket_list_[__chash], or __first_node if bucket is null
1780 __node_pointer __pn = __bucket_list_[__chash];
1781 if (__pn == nullptr)
1782 {
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001783 __pn = static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001784 __nd->__next_ = __pn->__next_;
1785 __pn->__next_ = __nd;
1786 // fix up __bucket_list_
1787 __bucket_list_[__chash] = __pn;
1788 if (__nd->__next_ != nullptr)
Howard Hinnant7a445152012-07-06 17:31:14 +00001789 __bucket_list_[__constrain_hash(__nd->__next_->__hash_, __bc)] = __nd;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001790 }
1791 else
1792 {
1793 __nd->__next_ = __pn->__next_;
1794 __pn->__next_ = __nd;
1795 }
1796 __ndptr = __nd;
1797 // increment size
1798 ++size();
1799 __inserted = true;
1800 }
1801__done:
Howard Hinnant39213642013-07-23 22:01:58 +00001802#if _LIBCPP_DEBUG_LEVEL >= 2
1803 return pair<iterator, bool>(iterator(__ndptr, this), __inserted);
1804#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001805 return pair<iterator, bool>(iterator(__ndptr), __inserted);
Howard Hinnant39213642013-07-23 22:01:58 +00001806#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001807}
1808
1809template <class _Tp, class _Hash, class _Equal, class _Alloc>
1810typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1811__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi(__node_pointer __cp)
1812{
1813 __cp->__hash_ = hash_function()(__cp->__value_);
1814 size_type __bc = bucket_count();
1815 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1816 {
Eric Fiselier57947ca2015-02-02 21:31:48 +00001817 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001818 size_type(ceil(float(size() + 1) / max_load_factor()))));
1819 __bc = bucket_count();
1820 }
Howard Hinnant7a445152012-07-06 17:31:14 +00001821 size_t __chash = __constrain_hash(__cp->__hash_, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001822 __node_pointer __pn = __bucket_list_[__chash];
1823 if (__pn == nullptr)
1824 {
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001825 __pn = static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001826 __cp->__next_ = __pn->__next_;
1827 __pn->__next_ = __cp;
1828 // fix up __bucket_list_
1829 __bucket_list_[__chash] = __pn;
1830 if (__cp->__next_ != nullptr)
Howard Hinnant7a445152012-07-06 17:31:14 +00001831 __bucket_list_[__constrain_hash(__cp->__next_->__hash_, __bc)] = __cp;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001832 }
1833 else
1834 {
1835 for (bool __found = false; __pn->__next_ != nullptr &&
Howard Hinnant7a445152012-07-06 17:31:14 +00001836 __constrain_hash(__pn->__next_->__hash_, __bc) == __chash;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001837 __pn = __pn->__next_)
Howard Hinnant324bb032010-08-22 00:02:43 +00001838 {
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001839 // __found key_eq() action
1840 // false false loop
1841 // true true loop
1842 // false true set __found to true
1843 // true false break
1844 if (__found != (__pn->__next_->__hash_ == __cp->__hash_ &&
1845 key_eq()(__pn->__next_->__value_, __cp->__value_)))
1846 {
1847 if (!__found)
1848 __found = true;
1849 else
1850 break;
1851 }
1852 }
1853 __cp->__next_ = __pn->__next_;
1854 __pn->__next_ = __cp;
1855 if (__cp->__next_ != nullptr)
1856 {
Howard Hinnant7a445152012-07-06 17:31:14 +00001857 size_t __nhash = __constrain_hash(__cp->__next_->__hash_, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001858 if (__nhash != __chash)
1859 __bucket_list_[__nhash] = __cp;
1860 }
1861 }
1862 ++size();
Howard Hinnant39213642013-07-23 22:01:58 +00001863#if _LIBCPP_DEBUG_LEVEL >= 2
1864 return iterator(__cp, this);
1865#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001866 return iterator(__cp);
Howard Hinnant39213642013-07-23 22:01:58 +00001867#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001868}
1869
1870template <class _Tp, class _Hash, class _Equal, class _Alloc>
1871typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1872__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi(
1873 const_iterator __p, __node_pointer __cp)
1874{
Howard Hinnant824c1992013-08-02 17:50:49 +00001875#if _LIBCPP_DEBUG_LEVEL >= 2
1876 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1877 "unordered container::emplace_hint(const_iterator, args...) called with an iterator not"
1878 " referring to this unordered container");
1879#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001880 if (__p != end() && key_eq()(*__p, __cp->__value_))
1881 {
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001882 __node_pointer __np = __p.__node_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001883 __cp->__hash_ = __np->__hash_;
1884 size_type __bc = bucket_count();
1885 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1886 {
Eric Fiselier57947ca2015-02-02 21:31:48 +00001887 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001888 size_type(ceil(float(size() + 1) / max_load_factor()))));
1889 __bc = bucket_count();
1890 }
Howard Hinnant7a445152012-07-06 17:31:14 +00001891 size_t __chash = __constrain_hash(__cp->__hash_, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001892 __node_pointer __pp = __bucket_list_[__chash];
1893 while (__pp->__next_ != __np)
1894 __pp = __pp->__next_;
1895 __cp->__next_ = __np;
1896 __pp->__next_ = __cp;
1897 ++size();
Howard Hinnant39213642013-07-23 22:01:58 +00001898#if _LIBCPP_DEBUG_LEVEL >= 2
1899 return iterator(__cp, this);
1900#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001901 return iterator(__cp);
Howard Hinnant39213642013-07-23 22:01:58 +00001902#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001903 }
1904 return __node_insert_multi(__cp);
1905}
1906
Eric Fiselierfdae69a2015-06-13 07:18:32 +00001907
1908
Eric Fiselier2960ae22016-02-11 11:59:44 +00001909#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierfdae69a2015-06-13 07:18:32 +00001910template <class _Tp, class _Hash, class _Equal, class _Alloc>
Eric Fiselier2960ae22016-02-11 11:59:44 +00001911template <class _Key, class ..._Args>
Eric Fiselierfdae69a2015-06-13 07:18:32 +00001912_LIBCPP_INLINE_VISIBILITY
1913pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
Eric Fiselier2960ae22016-02-11 11:59:44 +00001914__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_key_args(_Key const& __k, _Args&&... __args)
Eric Fiselierfdae69a2015-06-13 07:18:32 +00001915#else
1916template <class _Tp, class _Hash, class _Equal, class _Alloc>
Eric Fiselier2960ae22016-02-11 11:59:44 +00001917template <class _Key, class _Args>
Eric Fiselierfdae69a2015-06-13 07:18:32 +00001918_LIBCPP_INLINE_VISIBILITY
1919pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
Eric Fiselier2960ae22016-02-11 11:59:44 +00001920__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_key_args(_Key const& __k, _Args& __args)
Eric Fiselierfdae69a2015-06-13 07:18:32 +00001921#endif
1922{
Eric Fiselier2960ae22016-02-11 11:59:44 +00001923
1924 size_t __hash = hash_function()(__k);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001925 size_type __bc = bucket_count();
1926 bool __inserted = false;
1927 __node_pointer __nd;
1928 size_t __chash;
1929 if (__bc != 0)
1930 {
Howard Hinnant7a445152012-07-06 17:31:14 +00001931 __chash = __constrain_hash(__hash, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001932 __nd = __bucket_list_[__chash];
1933 if (__nd != nullptr)
1934 {
1935 for (__nd = __nd->__next_; __nd != nullptr &&
Howard Hinnant7a445152012-07-06 17:31:14 +00001936 __constrain_hash(__nd->__hash_, __bc) == __chash;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001937 __nd = __nd->__next_)
1938 {
Eric Fiselier2960ae22016-02-11 11:59:44 +00001939 if (key_eq()(__nd->__value_, __k))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001940 goto __done;
1941 }
1942 }
1943 }
1944 {
Eric Fiselier2960ae22016-02-11 11:59:44 +00001945#ifndef _LIBCPP_CXX03_LANG
1946 __node_holder __h = __construct_node_hash(__hash, _VSTD::forward<_Args>(__args)...);
1947#else
1948 __node_holder __h = __construct_node_hash(__hash, __args);
1949#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001950 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1951 {
Eric Fiselier57947ca2015-02-02 21:31:48 +00001952 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001953 size_type(ceil(float(size() + 1) / max_load_factor()))));
1954 __bc = bucket_count();
Howard Hinnant7a445152012-07-06 17:31:14 +00001955 __chash = __constrain_hash(__hash, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001956 }
1957 // insert_after __bucket_list_[__chash], or __first_node if bucket is null
1958 __node_pointer __pn = __bucket_list_[__chash];
1959 if (__pn == nullptr)
1960 {
Eric Fiselier9e9f42e2016-02-11 15:22:37 +00001961 __pn = static_cast<__node_pointer>(static_cast<__void_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first())));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001962 __h->__next_ = __pn->__next_;
1963 __pn->__next_ = __h.get();
1964 // fix up __bucket_list_
1965 __bucket_list_[__chash] = __pn;
1966 if (__h->__next_ != nullptr)
Howard Hinnant7a445152012-07-06 17:31:14 +00001967 __bucket_list_[__constrain_hash(__h->__next_->__hash_, __bc)] = __h.get();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001968 }
1969 else
1970 {
1971 __h->__next_ = __pn->__next_;
1972 __pn->__next_ = __h.get();
1973 }
1974 __nd = __h.release();
1975 // increment size
1976 ++size();
1977 __inserted = true;
1978 }
1979__done:
Howard Hinnant39213642013-07-23 22:01:58 +00001980#if _LIBCPP_DEBUG_LEVEL >= 2
1981 return pair<iterator, bool>(iterator(__nd, this), __inserted);
1982#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001983 return pair<iterator, bool>(iterator(__nd), __inserted);
Howard Hinnant39213642013-07-23 22:01:58 +00001984#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001985}
1986
Eric Fiselier2960ae22016-02-11 11:59:44 +00001987#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001988
1989template <class _Tp, class _Hash, class _Equal, class _Alloc>
1990template <class... _Args>
1991pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
1992__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique(_Args&&... __args)
1993{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001994 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001995 pair<iterator, bool> __r = __node_insert_unique(__h.get());
1996 if (__r.second)
1997 __h.release();
1998 return __r;
1999}
2000
2001template <class _Tp, class _Hash, class _Equal, class _Alloc>
2002template <class... _Args>
2003typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2004__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_multi(_Args&&... __args)
2005{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002006 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002007 iterator __r = __node_insert_multi(__h.get());
2008 __h.release();
2009 return __r;
2010}
2011
2012template <class _Tp, class _Hash, class _Equal, class _Alloc>
2013template <class... _Args>
2014typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2015__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_hint_multi(
2016 const_iterator __p, _Args&&... __args)
2017{
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00002018#if _LIBCPP_DEBUG_LEVEL >= 2
2019 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2020 "unordered container::emplace_hint(const_iterator, args...) called with an iterator not"
2021 " referring to this unordered container");
2022#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00002023 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002024 iterator __r = __node_insert_multi(__p, __h.get());
2025 __h.release();
2026 return __r;
2027}
2028
Eric Fiselier2960ae22016-02-11 11:59:44 +00002029#else // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002030
2031template <class _Tp, class _Hash, class _Equal, class _Alloc>
2032typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
Eric Fiselier2960ae22016-02-11 11:59:44 +00002033__hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const __container_value_type& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002034{
2035 __node_holder __h = __construct_node(__x);
2036 iterator __r = __node_insert_multi(__h.get());
2037 __h.release();
2038 return __r;
2039}
2040
2041template <class _Tp, class _Hash, class _Equal, class _Alloc>
2042typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2043__hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const_iterator __p,
Eric Fiselier2960ae22016-02-11 11:59:44 +00002044 const __container_value_type& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002045{
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00002046#if _LIBCPP_DEBUG_LEVEL >= 2
2047 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2048 "unordered container::insert(const_iterator, lvalue) called with an iterator not"
2049 " referring to this unordered container");
2050#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002051 __node_holder __h = __construct_node(__x);
2052 iterator __r = __node_insert_multi(__p, __h.get());
2053 __h.release();
2054 return __r;
2055}
2056
Eric Fiselier2960ae22016-02-11 11:59:44 +00002057#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002058
2059template <class _Tp, class _Hash, class _Equal, class _Alloc>
2060void
2061__hash_table<_Tp, _Hash, _Equal, _Alloc>::rehash(size_type __n)
2062{
Howard Hinnant7a445152012-07-06 17:31:14 +00002063 if (__n == 1)
2064 __n = 2;
2065 else if (__n & (__n - 1))
2066 __n = __next_prime(__n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002067 size_type __bc = bucket_count();
2068 if (__n > __bc)
2069 __rehash(__n);
Howard Hinnant7a445152012-07-06 17:31:14 +00002070 else if (__n < __bc)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002071 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002072 __n = _VSTD::max<size_type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002073 (
2074 __n,
Eric Fiselier57947ca2015-02-02 21:31:48 +00002075 __is_hash_power2(__bc) ? __next_hash_pow2(size_t(ceil(float(size()) / max_load_factor()))) :
2076 __next_prime(size_t(ceil(float(size()) / max_load_factor())))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002077 );
2078 if (__n < __bc)
2079 __rehash(__n);
2080 }
2081}
2082
2083template <class _Tp, class _Hash, class _Equal, class _Alloc>
2084void
2085__hash_table<_Tp, _Hash, _Equal, _Alloc>::__rehash(size_type __nbc)
2086{
Howard Hinnant39213642013-07-23 22:01:58 +00002087#if _LIBCPP_DEBUG_LEVEL >= 2
2088 __get_db()->__invalidate_all(this);
2089#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002090 __pointer_allocator& __npa = __bucket_list_.get_deleter().__alloc();
2091 __bucket_list_.reset(__nbc > 0 ?
2092 __pointer_alloc_traits::allocate(__npa, __nbc) : nullptr);
2093 __bucket_list_.get_deleter().size() = __nbc;
2094 if (__nbc > 0)
2095 {
2096 for (size_type __i = 0; __i < __nbc; ++__i)
2097 __bucket_list_[__i] = nullptr;
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00002098 __node_pointer __pp(static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first())));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002099 __node_pointer __cp = __pp->__next_;
2100 if (__cp != nullptr)
2101 {
Howard Hinnant7a445152012-07-06 17:31:14 +00002102 size_type __chash = __constrain_hash(__cp->__hash_, __nbc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002103 __bucket_list_[__chash] = __pp;
2104 size_type __phash = __chash;
2105 for (__pp = __cp, __cp = __cp->__next_; __cp != nullptr;
2106 __cp = __pp->__next_)
2107 {
Howard Hinnant7a445152012-07-06 17:31:14 +00002108 __chash = __constrain_hash(__cp->__hash_, __nbc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002109 if (__chash == __phash)
2110 __pp = __cp;
2111 else
2112 {
2113 if (__bucket_list_[__chash] == nullptr)
2114 {
2115 __bucket_list_[__chash] = __pp;
2116 __pp = __cp;
2117 __phash = __chash;
2118 }
2119 else
2120 {
2121 __node_pointer __np = __cp;
2122 for (; __np->__next_ != nullptr &&
2123 key_eq()(__cp->__value_, __np->__next_->__value_);
2124 __np = __np->__next_)
2125 ;
2126 __pp->__next_ = __np->__next_;
2127 __np->__next_ = __bucket_list_[__chash]->__next_;
2128 __bucket_list_[__chash]->__next_ = __cp;
Howard Hinnant324bb032010-08-22 00:02:43 +00002129
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002130 }
2131 }
2132 }
2133 }
2134 }
2135}
2136
2137template <class _Tp, class _Hash, class _Equal, class _Alloc>
2138template <class _Key>
2139typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2140__hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k)
2141{
2142 size_t __hash = hash_function()(__k);
2143 size_type __bc = bucket_count();
2144 if (__bc != 0)
2145 {
Howard Hinnant7a445152012-07-06 17:31:14 +00002146 size_t __chash = __constrain_hash(__hash, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002147 __node_pointer __nd = __bucket_list_[__chash];
2148 if (__nd != nullptr)
2149 {
2150 for (__nd = __nd->__next_; __nd != nullptr &&
Howard Hinnant7a445152012-07-06 17:31:14 +00002151 __constrain_hash(__nd->__hash_, __bc) == __chash;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002152 __nd = __nd->__next_)
2153 {
2154 if (key_eq()(__nd->__value_, __k))
Howard Hinnant39213642013-07-23 22:01:58 +00002155#if _LIBCPP_DEBUG_LEVEL >= 2
2156 return iterator(__nd, this);
2157#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002158 return iterator(__nd);
Howard Hinnant39213642013-07-23 22:01:58 +00002159#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002160 }
2161 }
2162 }
2163 return end();
2164}
2165
2166template <class _Tp, class _Hash, class _Equal, class _Alloc>
2167template <class _Key>
2168typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
2169__hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k) const
2170{
2171 size_t __hash = hash_function()(__k);
2172 size_type __bc = bucket_count();
2173 if (__bc != 0)
2174 {
Howard Hinnant7a445152012-07-06 17:31:14 +00002175 size_t __chash = __constrain_hash(__hash, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002176 __node_const_pointer __nd = __bucket_list_[__chash];
2177 if (__nd != nullptr)
2178 {
2179 for (__nd = __nd->__next_; __nd != nullptr &&
Howard Hinnant7a445152012-07-06 17:31:14 +00002180 __constrain_hash(__nd->__hash_, __bc) == __chash;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002181 __nd = __nd->__next_)
2182 {
2183 if (key_eq()(__nd->__value_, __k))
Howard Hinnant39213642013-07-23 22:01:58 +00002184#if _LIBCPP_DEBUG_LEVEL >= 2
2185 return const_iterator(__nd, this);
2186#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002187 return const_iterator(__nd);
Howard Hinnant39213642013-07-23 22:01:58 +00002188#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002189 }
2190 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002191
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002192 }
2193 return end();
2194}
2195
Eric Fiselier2960ae22016-02-11 11:59:44 +00002196#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002197
2198template <class _Tp, class _Hash, class _Equal, class _Alloc>
2199template <class ..._Args>
2200typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
2201__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(_Args&& ...__args)
2202{
Eric Fiselier2960ae22016-02-11 11:59:44 +00002203 static_assert(!__is_hash_value_type<_Args...>::value,
2204 "Construct cannot be called with a hash value type");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002205 __node_allocator& __na = __node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00002206 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselier2960ae22016-02-11 11:59:44 +00002207 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002208 __h.get_deleter().__value_constructed = true;
2209 __h->__hash_ = hash_function()(__h->__value_);
2210 __h->__next_ = nullptr;
2211 return __h;
2212}
2213
2214template <class _Tp, class _Hash, class _Equal, class _Alloc>
Eric Fiselier2960ae22016-02-11 11:59:44 +00002215template <class _First, class ..._Rest>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002216typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Eric Fiselier2960ae22016-02-11 11:59:44 +00002217__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node_hash(
2218 size_t __hash, _First&& __f, _Rest&& ...__rest)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002219{
Eric Fiselier2960ae22016-02-11 11:59:44 +00002220 static_assert(!__is_hash_value_type<_First, _Rest...>::value,
2221 "Construct cannot be called with a hash value type");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002222 __node_allocator& __na = __node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00002223 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselier2960ae22016-02-11 11:59:44 +00002224 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_),
2225 _VSTD::forward<_First>(__f),
2226 _VSTD::forward<_Rest>(__rest)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002227 __h.get_deleter().__value_constructed = true;
2228 __h->__hash_ = __hash;
2229 __h->__next_ = nullptr;
Howard Hinnant9a894d92013-08-22 18:29:50 +00002230 return __h;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002231}
2232
Eric Fiselier2960ae22016-02-11 11:59:44 +00002233#else // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002234
2235template <class _Tp, class _Hash, class _Equal, class _Alloc>
2236typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Eric Fiselier2960ae22016-02-11 11:59:44 +00002237__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(const __container_value_type& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002238{
2239 __node_allocator& __na = __node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00002240 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselier2960ae22016-02-11 11:59:44 +00002241 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002242 __h.get_deleter().__value_constructed = true;
2243 __h->__hash_ = hash_function()(__h->__value_);
2244 __h->__next_ = nullptr;
Dimitry Andric89663502015-08-19 06:43:33 +00002245 return _LIBCPP_EXPLICIT_MOVE(__h); // explicitly moved for C++03
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002246}
2247
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002248template <class _Tp, class _Hash, class _Equal, class _Alloc>
2249typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Eric Fiselier2960ae22016-02-11 11:59:44 +00002250__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node_hash(size_t __hash,
2251 const __container_value_type& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002252{
2253 __node_allocator& __na = __node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00002254 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselier2960ae22016-02-11 11:59:44 +00002255 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002256 __h.get_deleter().__value_constructed = true;
2257 __h->__hash_ = __hash;
2258 __h->__next_ = nullptr;
Dimitry Andric89663502015-08-19 06:43:33 +00002259 return _LIBCPP_EXPLICIT_MOVE(__h); // explicitly moved for C++03
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002260}
2261
Eric Fiselier2960ae22016-02-11 11:59:44 +00002262#endif // _LIBCPP_CXX03_LANG
2263
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002264template <class _Tp, class _Hash, class _Equal, class _Alloc>
2265typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2266__hash_table<_Tp, _Hash, _Equal, _Alloc>::erase(const_iterator __p)
2267{
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00002268 __node_pointer __np = __p.__node_;
Howard Hinnant39213642013-07-23 22:01:58 +00002269#if _LIBCPP_DEBUG_LEVEL >= 2
2270 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2271 "unordered container erase(iterator) called with an iterator not"
2272 " referring to this container");
2273 _LIBCPP_ASSERT(__p != end(),
2274 "unordered container erase(iterator) called with a non-dereferenceable iterator");
2275 iterator __r(__np, this);
2276#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002277 iterator __r(__np);
Howard Hinnant39213642013-07-23 22:01:58 +00002278#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002279 ++__r;
2280 remove(__p);
2281 return __r;
2282}
2283
2284template <class _Tp, class _Hash, class _Equal, class _Alloc>
2285typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2286__hash_table<_Tp, _Hash, _Equal, _Alloc>::erase(const_iterator __first,
2287 const_iterator __last)
2288{
Howard Hinnant39213642013-07-23 22:01:58 +00002289#if _LIBCPP_DEBUG_LEVEL >= 2
2290 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
2291 "unodered container::erase(iterator, iterator) called with an iterator not"
2292 " referring to this unodered container");
2293 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__last) == this,
2294 "unodered container::erase(iterator, iterator) called with an iterator not"
2295 " referring to this unodered container");
2296#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002297 for (const_iterator __p = __first; __first != __last; __p = __first)
2298 {
2299 ++__first;
2300 erase(__p);
2301 }
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00002302 __node_pointer __np = __last.__node_;
Howard Hinnant39213642013-07-23 22:01:58 +00002303#if _LIBCPP_DEBUG_LEVEL >= 2
2304 return iterator (__np, this);
2305#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002306 return iterator (__np);
Howard Hinnant39213642013-07-23 22:01:58 +00002307#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002308}
2309
2310template <class _Tp, class _Hash, class _Equal, class _Alloc>
2311template <class _Key>
2312typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2313__hash_table<_Tp, _Hash, _Equal, _Alloc>::__erase_unique(const _Key& __k)
2314{
2315 iterator __i = find(__k);
2316 if (__i == end())
2317 return 0;
2318 erase(__i);
2319 return 1;
2320}
2321
2322template <class _Tp, class _Hash, class _Equal, class _Alloc>
2323template <class _Key>
2324typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2325__hash_table<_Tp, _Hash, _Equal, _Alloc>::__erase_multi(const _Key& __k)
2326{
2327 size_type __r = 0;
2328 iterator __i = find(__k);
2329 if (__i != end())
2330 {
2331 iterator __e = end();
2332 do
2333 {
2334 erase(__i++);
2335 ++__r;
2336 } while (__i != __e && key_eq()(*__i, __k));
2337 }
2338 return __r;
2339}
2340
2341template <class _Tp, class _Hash, class _Equal, class _Alloc>
2342typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00002343__hash_table<_Tp, _Hash, _Equal, _Alloc>::remove(const_iterator __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002344{
2345 // current node
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00002346 __node_pointer __cn = __p.__node_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002347 size_type __bc = bucket_count();
Howard Hinnant7a445152012-07-06 17:31:14 +00002348 size_t __chash = __constrain_hash(__cn->__hash_, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002349 // find previous node
2350 __node_pointer __pn = __bucket_list_[__chash];
2351 for (; __pn->__next_ != __cn; __pn = __pn->__next_)
2352 ;
2353 // Fix up __bucket_list_
2354 // if __pn is not in same bucket (before begin is not in same bucket) &&
2355 // if __cn->__next_ is not in same bucket (nullptr is not in same bucket)
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00002356 if (__pn == static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()))
2357 || __constrain_hash(__pn->__hash_, __bc) != __chash)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002358 {
Howard Hinnant7a445152012-07-06 17:31:14 +00002359 if (__cn->__next_ == nullptr || __constrain_hash(__cn->__next_->__hash_, __bc) != __chash)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002360 __bucket_list_[__chash] = nullptr;
2361 }
2362 // if __cn->__next_ is not in same bucket (nullptr is in same bucket)
2363 if (__cn->__next_ != nullptr)
2364 {
Howard Hinnant7a445152012-07-06 17:31:14 +00002365 size_t __nhash = __constrain_hash(__cn->__next_->__hash_, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002366 if (__nhash != __chash)
2367 __bucket_list_[__nhash] = __pn;
2368 }
2369 // remove __cn
2370 __pn->__next_ = __cn->__next_;
2371 __cn->__next_ = nullptr;
2372 --size();
Howard Hinnant39213642013-07-23 22:01:58 +00002373#if _LIBCPP_DEBUG_LEVEL >= 2
2374 __c_node* __c = __get_db()->__find_c_and_lock(this);
2375 for (__i_node** __p = __c->end_; __p != __c->beg_; )
2376 {
2377 --__p;
2378 iterator* __i = static_cast<iterator*>((*__p)->__i_);
2379 if (__i->__node_ == __cn)
2380 {
2381 (*__p)->__c_ = nullptr;
2382 if (--__c->end_ != __p)
2383 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
2384 }
2385 }
2386 __get_db()->unlock();
2387#endif
Howard Hinnant99968442011-11-29 18:15:50 +00002388 return __node_holder(__cn, _Dp(__node_alloc(), true));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002389}
2390
2391template <class _Tp, class _Hash, class _Equal, class _Alloc>
2392template <class _Key>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00002393inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002394typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2395__hash_table<_Tp, _Hash, _Equal, _Alloc>::__count_unique(const _Key& __k) const
2396{
2397 return static_cast<size_type>(find(__k) != end());
2398}
2399
2400template <class _Tp, class _Hash, class _Equal, class _Alloc>
2401template <class _Key>
2402typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2403__hash_table<_Tp, _Hash, _Equal, _Alloc>::__count_multi(const _Key& __k) const
2404{
2405 size_type __r = 0;
2406 const_iterator __i = find(__k);
2407 if (__i != end())
2408 {
2409 const_iterator __e = end();
2410 do
2411 {
2412 ++__i;
2413 ++__r;
2414 } while (__i != __e && key_eq()(*__i, __k));
2415 }
2416 return __r;
2417}
2418
2419template <class _Tp, class _Hash, class _Equal, class _Alloc>
2420template <class _Key>
2421pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator,
2422 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator>
2423__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_unique(
2424 const _Key& __k)
2425{
2426 iterator __i = find(__k);
2427 iterator __j = __i;
2428 if (__i != end())
2429 ++__j;
2430 return pair<iterator, iterator>(__i, __j);
2431}
2432
2433template <class _Tp, class _Hash, class _Equal, class _Alloc>
2434template <class _Key>
2435pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator,
2436 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator>
2437__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_unique(
2438 const _Key& __k) const
2439{
2440 const_iterator __i = find(__k);
2441 const_iterator __j = __i;
2442 if (__i != end())
2443 ++__j;
2444 return pair<const_iterator, const_iterator>(__i, __j);
2445}
2446
2447template <class _Tp, class _Hash, class _Equal, class _Alloc>
2448template <class _Key>
2449pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator,
2450 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator>
2451__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_multi(
2452 const _Key& __k)
2453{
2454 iterator __i = find(__k);
2455 iterator __j = __i;
2456 if (__i != end())
2457 {
2458 iterator __e = end();
2459 do
2460 {
2461 ++__j;
2462 } while (__j != __e && key_eq()(*__j, __k));
2463 }
2464 return pair<iterator, iterator>(__i, __j);
2465}
2466
2467template <class _Tp, class _Hash, class _Equal, class _Alloc>
2468template <class _Key>
2469pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator,
2470 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator>
2471__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_multi(
2472 const _Key& __k) const
2473{
2474 const_iterator __i = find(__k);
2475 const_iterator __j = __i;
2476 if (__i != end())
2477 {
2478 const_iterator __e = end();
2479 do
2480 {
2481 ++__j;
2482 } while (__j != __e && key_eq()(*__j, __k));
2483 }
2484 return pair<const_iterator, const_iterator>(__i, __j);
2485}
2486
2487template <class _Tp, class _Hash, class _Equal, class _Alloc>
2488void
2489__hash_table<_Tp, _Hash, _Equal, _Alloc>::swap(__hash_table& __u)
Eric Fiselier692177d2015-07-18 20:40:46 +00002490#if _LIBCPP_STD_VER <= 11
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00002491 _NOEXCEPT_(
Marshall Clow7d914d12015-07-13 20:04:56 +00002492 __is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value
Marshall Clow7d914d12015-07-13 20:04:56 +00002493 && (!allocator_traits<__pointer_allocator>::propagate_on_container_swap::value
2494 || __is_nothrow_swappable<__pointer_allocator>::value)
2495 && (!__node_traits::propagate_on_container_swap::value
2496 || __is_nothrow_swappable<__node_allocator>::value)
Marshall Clow7d914d12015-07-13 20:04:56 +00002497 )
Eric Fiselier692177d2015-07-18 20:40:46 +00002498#else
2499 _NOEXCEPT_(__is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value)
2500#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002501{
2502 {
2503 __node_pointer_pointer __npp = __bucket_list_.release();
2504 __bucket_list_.reset(__u.__bucket_list_.release());
2505 __u.__bucket_list_.reset(__npp);
2506 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002507 _VSTD::swap(__bucket_list_.get_deleter().size(), __u.__bucket_list_.get_deleter().size());
Marshall Clow7d914d12015-07-13 20:04:56 +00002508 __swap_allocator(__bucket_list_.get_deleter().__alloc(),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002509 __u.__bucket_list_.get_deleter().__alloc());
Marshall Clow7d914d12015-07-13 20:04:56 +00002510 __swap_allocator(__node_alloc(), __u.__node_alloc());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002511 _VSTD::swap(__p1_.first().__next_, __u.__p1_.first().__next_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002512 __p2_.swap(__u.__p2_);
2513 __p3_.swap(__u.__p3_);
2514 if (size() > 0)
Howard Hinnant7a445152012-07-06 17:31:14 +00002515 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash_, bucket_count())] =
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00002516 static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002517 if (__u.size() > 0)
Howard Hinnant7a445152012-07-06 17:31:14 +00002518 __u.__bucket_list_[__constrain_hash(__u.__p1_.first().__next_->__hash_, __u.bucket_count())] =
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00002519 static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__u.__p1_.first()));
Howard Hinnant39213642013-07-23 22:01:58 +00002520#if _LIBCPP_DEBUG_LEVEL >= 2
2521 __get_db()->swap(this, &__u);
2522#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002523}
2524
2525template <class _Tp, class _Hash, class _Equal, class _Alloc>
2526typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2527__hash_table<_Tp, _Hash, _Equal, _Alloc>::bucket_size(size_type __n) const
2528{
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00002529 _LIBCPP_ASSERT(__n < bucket_count(),
2530 "unordered container::bucket_size(n) called with n >= bucket_count()");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002531 __node_const_pointer __np = __bucket_list_[__n];
2532 size_type __bc = bucket_count();
2533 size_type __r = 0;
2534 if (__np != nullptr)
2535 {
2536 for (__np = __np->__next_; __np != nullptr &&
Howard Hinnant7a445152012-07-06 17:31:14 +00002537 __constrain_hash(__np->__hash_, __bc) == __n;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002538 __np = __np->__next_, ++__r)
2539 ;
2540 }
2541 return __r;
2542}
2543
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00002544template <class _Tp, class _Hash, class _Equal, class _Alloc>
2545inline _LIBCPP_INLINE_VISIBILITY
2546void
2547swap(__hash_table<_Tp, _Hash, _Equal, _Alloc>& __x,
2548 __hash_table<_Tp, _Hash, _Equal, _Alloc>& __y)
2549 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
2550{
2551 __x.swap(__y);
2552}
2553
Howard Hinnant39213642013-07-23 22:01:58 +00002554#if _LIBCPP_DEBUG_LEVEL >= 2
2555
2556template <class _Tp, class _Hash, class _Equal, class _Alloc>
2557bool
2558__hash_table<_Tp, _Hash, _Equal, _Alloc>::__dereferenceable(const const_iterator* __i) const
2559{
2560 return __i->__node_ != nullptr;
2561}
2562
2563template <class _Tp, class _Hash, class _Equal, class _Alloc>
2564bool
2565__hash_table<_Tp, _Hash, _Equal, _Alloc>::__decrementable(const const_iterator*) const
2566{
2567 return false;
2568}
2569
2570template <class _Tp, class _Hash, class _Equal, class _Alloc>
2571bool
2572__hash_table<_Tp, _Hash, _Equal, _Alloc>::__addable(const const_iterator*, ptrdiff_t) const
2573{
2574 return false;
2575}
2576
2577template <class _Tp, class _Hash, class _Equal, class _Alloc>
2578bool
2579__hash_table<_Tp, _Hash, _Equal, _Alloc>::__subscriptable(const const_iterator*, ptrdiff_t) const
2580{
2581 return false;
2582}
2583
2584#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002585_LIBCPP_END_NAMESPACE_STD
2586
2587#endif // _LIBCPP__HASH_TABLE