blob: c35c6dfcdf596827f119ab609afec6d83aaa2d4b [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;
383 typedef __hash_iterator<_NodePtr> __non_const_iterator;
384 __node_pointer __node_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000385
386public:
Eric Fiselier774c7c52016-02-10 20:46:23 +0000387 typedef forward_iterator_tag iterator_category;
388 typedef typename _NodeTypes::__node_value_type value_type;
389 typedef typename _NodeTypes::difference_type difference_type;
390 typedef const value_type& reference;
391 typedef typename _NodeTypes::__const_node_value_type_pointer pointer;
392
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000393
Howard Hinnant39213642013-07-23 22:01:58 +0000394 _LIBCPP_INLINE_VISIBILITY __hash_const_iterator() _NOEXCEPT
Marshall Clow193ef032013-08-07 21:30:44 +0000395#if _LIBCPP_STD_VER > 11
396 : __node_(nullptr)
397#endif
Howard Hinnant39213642013-07-23 22:01:58 +0000398 {
399#if _LIBCPP_DEBUG_LEVEL >= 2
400 __get_db()->__insert_i(this);
401#endif
402 }
Howard Hinnant99acc502010-09-21 17:32:39 +0000403 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000404 __hash_const_iterator(const __non_const_iterator& __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000405 : __node_(__x.__node_)
Howard Hinnant39213642013-07-23 22:01:58 +0000406 {
407#if _LIBCPP_DEBUG_LEVEL >= 2
408 __get_db()->__iterator_copy(this, &__x);
409#endif
410 }
411
412#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000413
Howard Hinnant99acc502010-09-21 17:32:39 +0000414 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000415 __hash_const_iterator(const __hash_const_iterator& __i)
416 : __node_(__i.__node_)
417 {
418 __get_db()->__iterator_copy(this, &__i);
419 }
420
Howard Hinnant99acc502010-09-21 17:32:39 +0000421 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000422 ~__hash_const_iterator()
423 {
424 __get_db()->__erase_i(this);
425 }
426
427 _LIBCPP_INLINE_VISIBILITY
428 __hash_const_iterator& operator=(const __hash_const_iterator& __i)
429 {
430 if (this != &__i)
431 {
432 __get_db()->__iterator_copy(this, &__i);
433 __node_ = __i.__node_;
434 }
435 return *this;
436 }
437
438#endif // _LIBCPP_DEBUG_LEVEL >= 2
439
440 _LIBCPP_INLINE_VISIBILITY
441 reference operator*() const
442 {
443#if _LIBCPP_DEBUG_LEVEL >= 2
444 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
445 "Attempted to dereference a non-dereferenceable unordered container const_iterator");
446#endif
447 return __node_->__value_;
448 }
449 _LIBCPP_INLINE_VISIBILITY
450 pointer operator->() const
451 {
452#if _LIBCPP_DEBUG_LEVEL >= 2
453 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
454 "Attempted to dereference a non-dereferenceable unordered container const_iterator");
455#endif
456 return pointer_traits<pointer>::pointer_to(__node_->__value_);
457 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000458
Howard Hinnant99acc502010-09-21 17:32:39 +0000459 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000460 __hash_const_iterator& operator++()
461 {
Howard Hinnant39213642013-07-23 22:01:58 +0000462#if _LIBCPP_DEBUG_LEVEL >= 2
463 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
464 "Attempted to increment non-incrementable unordered container const_iterator");
465#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000466 __node_ = __node_->__next_;
467 return *this;
468 }
469
Howard Hinnant99acc502010-09-21 17:32:39 +0000470 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000471 __hash_const_iterator operator++(int)
472 {
473 __hash_const_iterator __t(*this);
474 ++(*this);
475 return __t;
476 }
477
Howard Hinnant99acc502010-09-21 17:32:39 +0000478 friend _LIBCPP_INLINE_VISIBILITY
479 bool operator==(const __hash_const_iterator& __x, const __hash_const_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58 +0000480 {
Howard Hinnant39213642013-07-23 22:01:58 +0000481 return __x.__node_ == __y.__node_;
482 }
Howard Hinnant99acc502010-09-21 17:32:39 +0000483 friend _LIBCPP_INLINE_VISIBILITY
484 bool operator!=(const __hash_const_iterator& __x, const __hash_const_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58 +0000485 {return !(__x == __y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000486
487private:
Howard Hinnant39213642013-07-23 22:01:58 +0000488#if _LIBCPP_DEBUG_LEVEL >= 2
489 _LIBCPP_INLINE_VISIBILITY
490 __hash_const_iterator(__node_pointer __node, const void* __c) _NOEXCEPT
491 : __node_(__node)
492 {
493 __get_db()->__insert_ic(this, __c);
494 }
495#else
Howard Hinnant99acc502010-09-21 17:32:39 +0000496 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000497 __hash_const_iterator(__node_pointer __node) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000498 : __node_(__node)
499 {}
Howard Hinnant39213642013-07-23 22:01:58 +0000500#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000501
502 template <class, class, class, class> friend class __hash_table;
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000503 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_map_const_iterator;
504 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_map;
505 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_multimap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000506};
507
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000508template <class _NodePtr>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000509class _LIBCPP_TYPE_VIS_ONLY __hash_local_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000510{
Eric Fiselier774c7c52016-02-10 20:46:23 +0000511 typedef __hash_node_types<_NodePtr> _NodeTypes;
512 typedef _NodePtr __node_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000513
514 __node_pointer __node_;
515 size_t __bucket_;
516 size_t __bucket_count_;
517
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000518public:
519 typedef forward_iterator_tag iterator_category;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000520 typedef typename _NodeTypes::__node_value_type value_type;
521 typedef typename _NodeTypes::difference_type difference_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000522 typedef value_type& reference;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000523 typedef typename _NodeTypes::__node_value_type_pointer pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000524
Howard Hinnant39213642013-07-23 22:01:58 +0000525 _LIBCPP_INLINE_VISIBILITY __hash_local_iterator() _NOEXCEPT
526 {
527#if _LIBCPP_DEBUG_LEVEL >= 2
528 __get_db()->__insert_i(this);
529#endif
530 }
531
532#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000533
Howard Hinnant99acc502010-09-21 17:32:39 +0000534 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000535 __hash_local_iterator(const __hash_local_iterator& __i)
536 : __node_(__i.__node_),
537 __bucket_(__i.__bucket_),
538 __bucket_count_(__i.__bucket_count_)
539 {
540 __get_db()->__iterator_copy(this, &__i);
541 }
542
Howard Hinnant99acc502010-09-21 17:32:39 +0000543 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000544 ~__hash_local_iterator()
545 {
546 __get_db()->__erase_i(this);
547 }
548
549 _LIBCPP_INLINE_VISIBILITY
550 __hash_local_iterator& operator=(const __hash_local_iterator& __i)
551 {
552 if (this != &__i)
553 {
554 __get_db()->__iterator_copy(this, &__i);
555 __node_ = __i.__node_;
556 __bucket_ = __i.__bucket_;
557 __bucket_count_ = __i.__bucket_count_;
558 }
559 return *this;
560 }
561
562#endif // _LIBCPP_DEBUG_LEVEL >= 2
563
564 _LIBCPP_INLINE_VISIBILITY
565 reference operator*() const
566 {
567#if _LIBCPP_DEBUG_LEVEL >= 2
568 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
569 "Attempted to dereference a non-dereferenceable unordered container local_iterator");
570#endif
571 return __node_->__value_;
572 }
573 _LIBCPP_INLINE_VISIBILITY
574 pointer operator->() const
575 {
576#if _LIBCPP_DEBUG_LEVEL >= 2
577 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
578 "Attempted to dereference a non-dereferenceable unordered container local_iterator");
579#endif
580 return pointer_traits<pointer>::pointer_to(__node_->__value_);
581 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000582
Howard Hinnant99acc502010-09-21 17:32:39 +0000583 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000584 __hash_local_iterator& operator++()
585 {
Howard Hinnant39213642013-07-23 22:01:58 +0000586#if _LIBCPP_DEBUG_LEVEL >= 2
587 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
588 "Attempted to increment non-incrementable unordered container local_iterator");
589#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000590 __node_ = __node_->__next_;
Howard Hinnant7a445152012-07-06 17:31:14 +0000591 if (__node_ != nullptr && __constrain_hash(__node_->__hash_, __bucket_count_) != __bucket_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000592 __node_ = nullptr;
593 return *this;
594 }
595
Howard Hinnant99acc502010-09-21 17:32:39 +0000596 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000597 __hash_local_iterator operator++(int)
598 {
599 __hash_local_iterator __t(*this);
600 ++(*this);
601 return __t;
602 }
603
Howard Hinnant99acc502010-09-21 17:32:39 +0000604 friend _LIBCPP_INLINE_VISIBILITY
605 bool operator==(const __hash_local_iterator& __x, const __hash_local_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58 +0000606 {
Howard Hinnant39213642013-07-23 22:01:58 +0000607 return __x.__node_ == __y.__node_;
608 }
Howard Hinnant99acc502010-09-21 17:32:39 +0000609 friend _LIBCPP_INLINE_VISIBILITY
610 bool operator!=(const __hash_local_iterator& __x, const __hash_local_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58 +0000611 {return !(__x == __y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000612
613private:
Howard Hinnant39213642013-07-23 22:01:58 +0000614#if _LIBCPP_DEBUG_LEVEL >= 2
615 _LIBCPP_INLINE_VISIBILITY
616 __hash_local_iterator(__node_pointer __node, size_t __bucket,
617 size_t __bucket_count, const void* __c) _NOEXCEPT
618 : __node_(__node),
619 __bucket_(__bucket),
620 __bucket_count_(__bucket_count)
621 {
622 __get_db()->__insert_ic(this, __c);
623 if (__node_ != nullptr)
624 __node_ = __node_->__next_;
625 }
626#else
Howard Hinnant99acc502010-09-21 17:32:39 +0000627 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000628 __hash_local_iterator(__node_pointer __node, size_t __bucket,
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000629 size_t __bucket_count) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000630 : __node_(__node),
631 __bucket_(__bucket),
632 __bucket_count_(__bucket_count)
633 {
634 if (__node_ != nullptr)
635 __node_ = __node_->__next_;
636 }
Howard Hinnant39213642013-07-23 22:01:58 +0000637#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000638 template <class, class, class, class> friend class __hash_table;
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000639 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_const_local_iterator;
640 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_map_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000641};
642
643template <class _ConstNodePtr>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000644class _LIBCPP_TYPE_VIS_ONLY __hash_const_local_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000645{
Eric Fiselier774c7c52016-02-10 20:46:23 +0000646 typedef __hash_node_types<_ConstNodePtr> _NodeTypes;
647 typedef _ConstNodePtr __node_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000648
649 __node_pointer __node_;
650 size_t __bucket_;
651 size_t __bucket_count_;
652
653 typedef pointer_traits<__node_pointer> __pointer_traits;
654 typedef typename __pointer_traits::element_type __node;
655 typedef typename remove_const<__node>::type __non_const_node;
Eric Fiselier5cf84e02015-12-30 21:52:00 +0000656 typedef typename __rebind_pointer<__node_pointer, __non_const_node>::type
657 __non_const_node_pointer;
658
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000659 typedef __hash_local_iterator<__non_const_node_pointer>
660 __non_const_iterator;
661public:
Eric Fiselier774c7c52016-02-10 20:46:23 +0000662 typedef forward_iterator_tag iterator_category;
663 typedef typename _NodeTypes::__node_value_type value_type;
664 typedef typename _NodeTypes::difference_type difference_type;
665 typedef const value_type& reference;
666 typedef typename _NodeTypes::__const_node_value_type_pointer pointer;
Eric Fiselier5cf84e02015-12-30 21:52:00 +0000667
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000668
Howard Hinnant39213642013-07-23 22:01:58 +0000669 _LIBCPP_INLINE_VISIBILITY __hash_const_local_iterator() _NOEXCEPT
670 {
671#if _LIBCPP_DEBUG_LEVEL >= 2
672 __get_db()->__insert_i(this);
673#endif
674 }
675
Howard Hinnant99acc502010-09-21 17:32:39 +0000676 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000677 __hash_const_local_iterator(const __non_const_iterator& __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000678 : __node_(__x.__node_),
679 __bucket_(__x.__bucket_),
680 __bucket_count_(__x.__bucket_count_)
Howard Hinnant39213642013-07-23 22:01:58 +0000681 {
682#if _LIBCPP_DEBUG_LEVEL >= 2
683 __get_db()->__iterator_copy(this, &__x);
684#endif
685 }
686
687#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000688
Howard Hinnant99acc502010-09-21 17:32:39 +0000689 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000690 __hash_const_local_iterator(const __hash_const_local_iterator& __i)
691 : __node_(__i.__node_),
692 __bucket_(__i.__bucket_),
693 __bucket_count_(__i.__bucket_count_)
694 {
695 __get_db()->__iterator_copy(this, &__i);
696 }
697
Howard Hinnant99acc502010-09-21 17:32:39 +0000698 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000699 ~__hash_const_local_iterator()
700 {
701 __get_db()->__erase_i(this);
702 }
703
704 _LIBCPP_INLINE_VISIBILITY
705 __hash_const_local_iterator& operator=(const __hash_const_local_iterator& __i)
706 {
707 if (this != &__i)
708 {
709 __get_db()->__iterator_copy(this, &__i);
710 __node_ = __i.__node_;
711 __bucket_ = __i.__bucket_;
712 __bucket_count_ = __i.__bucket_count_;
713 }
714 return *this;
715 }
716
717#endif // _LIBCPP_DEBUG_LEVEL >= 2
718
719 _LIBCPP_INLINE_VISIBILITY
720 reference operator*() const
721 {
722#if _LIBCPP_DEBUG_LEVEL >= 2
723 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
724 "Attempted to dereference a non-dereferenceable unordered container const_local_iterator");
725#endif
726 return __node_->__value_;
727 }
728 _LIBCPP_INLINE_VISIBILITY
729 pointer operator->() const
730 {
731#if _LIBCPP_DEBUG_LEVEL >= 2
732 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
733 "Attempted to dereference a non-dereferenceable unordered container const_local_iterator");
734#endif
735 return pointer_traits<pointer>::pointer_to(__node_->__value_);
736 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000737
Howard Hinnant99acc502010-09-21 17:32:39 +0000738 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000739 __hash_const_local_iterator& operator++()
740 {
Howard Hinnant39213642013-07-23 22:01:58 +0000741#if _LIBCPP_DEBUG_LEVEL >= 2
742 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
743 "Attempted to increment non-incrementable unordered container const_local_iterator");
744#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000745 __node_ = __node_->__next_;
Howard Hinnant7a445152012-07-06 17:31:14 +0000746 if (__node_ != nullptr && __constrain_hash(__node_->__hash_, __bucket_count_) != __bucket_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000747 __node_ = nullptr;
748 return *this;
749 }
750
Howard Hinnant99acc502010-09-21 17:32:39 +0000751 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000752 __hash_const_local_iterator operator++(int)
753 {
754 __hash_const_local_iterator __t(*this);
755 ++(*this);
756 return __t;
757 }
758
Howard Hinnant99acc502010-09-21 17:32:39 +0000759 friend _LIBCPP_INLINE_VISIBILITY
760 bool operator==(const __hash_const_local_iterator& __x, const __hash_const_local_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58 +0000761 {
Howard Hinnant39213642013-07-23 22:01:58 +0000762 return __x.__node_ == __y.__node_;
763 }
Howard Hinnant99acc502010-09-21 17:32:39 +0000764 friend _LIBCPP_INLINE_VISIBILITY
765 bool operator!=(const __hash_const_local_iterator& __x, const __hash_const_local_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58 +0000766 {return !(__x == __y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000767
768private:
Howard Hinnant39213642013-07-23 22:01:58 +0000769#if _LIBCPP_DEBUG_LEVEL >= 2
770 _LIBCPP_INLINE_VISIBILITY
771 __hash_const_local_iterator(__node_pointer __node, size_t __bucket,
772 size_t __bucket_count, const void* __c) _NOEXCEPT
773 : __node_(__node),
774 __bucket_(__bucket),
775 __bucket_count_(__bucket_count)
776 {
777 __get_db()->__insert_ic(this, __c);
778 if (__node_ != nullptr)
779 __node_ = __node_->__next_;
780 }
781#else
Howard Hinnant99acc502010-09-21 17:32:39 +0000782 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000783 __hash_const_local_iterator(__node_pointer __node, size_t __bucket,
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000784 size_t __bucket_count) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000785 : __node_(__node),
786 __bucket_(__bucket),
787 __bucket_count_(__bucket_count)
788 {
789 if (__node_ != nullptr)
790 __node_ = __node_->__next_;
791 }
Howard Hinnant39213642013-07-23 22:01:58 +0000792#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000793 template <class, class, class, class> friend class __hash_table;
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000794 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_map_const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000795};
796
797template <class _Alloc>
798class __bucket_list_deallocator
799{
800 typedef _Alloc allocator_type;
801 typedef allocator_traits<allocator_type> __alloc_traits;
802 typedef typename __alloc_traits::size_type size_type;
803
804 __compressed_pair<size_type, allocator_type> __data_;
805public:
806 typedef typename __alloc_traits::pointer pointer;
807
Howard Hinnant99acc502010-09-21 17:32:39 +0000808 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000809 __bucket_list_deallocator()
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000810 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000811 : __data_(0) {}
Howard Hinnant99acc502010-09-21 17:32:39 +0000812
813 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000814 __bucket_list_deallocator(const allocator_type& __a, size_type __size)
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000815 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000816 : __data_(__size, __a) {}
817
Howard Hinnant73d21a42010-09-04 23:28:19 +0000818#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000819
Howard Hinnant99acc502010-09-21 17:32:39 +0000820 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000821 __bucket_list_deallocator(__bucket_list_deallocator&& __x)
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000822 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000823 : __data_(_VSTD::move(__x.__data_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000824 {
825 __x.size() = 0;
826 }
827
Howard Hinnant73d21a42010-09-04 23:28:19 +0000828#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000829
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000830 _LIBCPP_INLINE_VISIBILITY
831 size_type& size() _NOEXCEPT {return __data_.first();}
832 _LIBCPP_INLINE_VISIBILITY
833 size_type size() const _NOEXCEPT {return __data_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000834
Howard Hinnant99acc502010-09-21 17:32:39 +0000835 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000836 allocator_type& __alloc() _NOEXCEPT {return __data_.second();}
837 _LIBCPP_INLINE_VISIBILITY
838 const allocator_type& __alloc() const _NOEXCEPT {return __data_.second();}
839
840 _LIBCPP_INLINE_VISIBILITY
841 void operator()(pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000842 {
843 __alloc_traits::deallocate(__alloc(), __p, size());
844 }
845};
846
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000847template <class _Alloc> class __hash_map_node_destructor;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000848
849template <class _Alloc>
850class __hash_node_destructor
851{
852 typedef _Alloc allocator_type;
853 typedef allocator_traits<allocator_type> __alloc_traits;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000854
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000855public:
856 typedef typename __alloc_traits::pointer pointer;
857private:
Eric Fiselier2960ae22016-02-11 11:59:44 +0000858 typedef __hash_node_types<pointer> _NodeTypes;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000859
860 allocator_type& __na_;
861
862 __hash_node_destructor& operator=(const __hash_node_destructor&);
863
864public:
865 bool __value_constructed;
866
Howard Hinnant99acc502010-09-21 17:32:39 +0000867 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant199d0ae2011-07-31 17:04:30 +0000868 explicit __hash_node_destructor(allocator_type& __na,
869 bool __constructed = false) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000870 : __na_(__na),
Howard Hinnant199d0ae2011-07-31 17:04:30 +0000871 __value_constructed(__constructed)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000872 {}
873
Howard Hinnant99acc502010-09-21 17:32:39 +0000874 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000875 void operator()(pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000876 {
877 if (__value_constructed)
Eric Fiselier2960ae22016-02-11 11:59:44 +0000878 __alloc_traits::destroy(__na_, _NodeTypes::__get_ptr(__p->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000879 if (__p)
880 __alloc_traits::deallocate(__na_, __p, 1);
881 }
882
883 template <class> friend class __hash_map_node_destructor;
884};
885
886template <class _Tp, class _Hash, class _Equal, class _Alloc>
887class __hash_table
888{
889public:
890 typedef _Tp value_type;
891 typedef _Hash hasher;
892 typedef _Equal key_equal;
893 typedef _Alloc allocator_type;
894
895private:
896 typedef allocator_traits<allocator_type> __alloc_traits;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000897 typedef typename
898 __make_hash_node_types<value_type, typename __alloc_traits::void_pointer>::type
899 _NodeTypes;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000900public:
Eric Fiselier2960ae22016-02-11 11:59:44 +0000901
902 typedef typename _NodeTypes::__node_value_type __node_value_type;
903 typedef typename _NodeTypes::__container_value_type __container_value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000904 typedef value_type& reference;
905 typedef const value_type& const_reference;
906 typedef typename __alloc_traits::pointer pointer;
907 typedef typename __alloc_traits::const_pointer const_pointer;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000908#ifndef _LIBCPP_ABI_FIX_UNORDERED_CONTAINER_SIZE_TYPE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000909 typedef typename __alloc_traits::size_type size_type;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000910#else
911 typedef typename _NodeTypes::size_type size_type;
912#endif
913 typedef typename _NodeTypes::difference_type difference_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000914public:
915 // Create __node
Eric Fiselier774c7c52016-02-10 20:46:23 +0000916
917 typedef typename _NodeTypes::__node_type __node;
Marshall Clow66302c62015-04-07 05:21:38 +0000918 typedef typename __rebind_alloc_helper<__alloc_traits, __node>::type __node_allocator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000919 typedef allocator_traits<__node_allocator> __node_traits;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000920 typedef typename _NodeTypes::__node_pointer __node_pointer;
921 typedef typename _NodeTypes::__node_pointer __node_const_pointer;
922 typedef typename _NodeTypes::__node_base_type __first_node;
923 typedef typename _NodeTypes::__node_base_pointer __node_base_pointer;
924
925private:
926 // check for sane allocator pointer rebinding semantics. Rebinding the
927 // allocator for a new pointer type should be exactly the same as rebinding
928 // the pointer using 'pointer_traits'.
929 static_assert((is_same<__node_pointer, typename __node_traits::pointer>::value),
930 "Allocator does not rebind pointers in a sane manner.");
931 typedef typename __rebind_alloc_helper<__node_traits, __first_node>::type
932 __node_base_allocator;
933 typedef allocator_traits<__node_base_allocator> __node_base_traits;
934 static_assert((is_same<__node_base_pointer, typename __node_base_traits::pointer>::value),
935 "Allocator does not rebind pointers in a sane manner.");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000936
937private:
938
Marshall Clow66302c62015-04-07 05:21:38 +0000939 typedef typename __rebind_alloc_helper<__node_traits, __node_pointer>::type __pointer_allocator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000940 typedef __bucket_list_deallocator<__pointer_allocator> __bucket_list_deleter;
941 typedef unique_ptr<__node_pointer[], __bucket_list_deleter> __bucket_list;
942 typedef allocator_traits<__pointer_allocator> __pointer_alloc_traits;
943 typedef typename __bucket_list_deleter::pointer __node_pointer_pointer;
944
945 // --- Member data begin ---
Eric Fiselier774c7c52016-02-10 20:46:23 +0000946 __bucket_list __bucket_list_;
947 __compressed_pair<__first_node, __node_allocator> __p1_;
948 __compressed_pair<size_type, hasher> __p2_;
949 __compressed_pair<float, key_equal> __p3_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000950 // --- Member data end ---
951
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000952 _LIBCPP_INLINE_VISIBILITY
953 size_type& size() _NOEXCEPT {return __p2_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000954public:
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000955 _LIBCPP_INLINE_VISIBILITY
956 size_type size() const _NOEXCEPT {return __p2_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000957
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000958 _LIBCPP_INLINE_VISIBILITY
959 hasher& hash_function() _NOEXCEPT {return __p2_.second();}
960 _LIBCPP_INLINE_VISIBILITY
961 const hasher& hash_function() const _NOEXCEPT {return __p2_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000962
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000963 _LIBCPP_INLINE_VISIBILITY
964 float& max_load_factor() _NOEXCEPT {return __p3_.first();}
965 _LIBCPP_INLINE_VISIBILITY
966 float max_load_factor() const _NOEXCEPT {return __p3_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000967
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000968 _LIBCPP_INLINE_VISIBILITY
969 key_equal& key_eq() _NOEXCEPT {return __p3_.second();}
970 _LIBCPP_INLINE_VISIBILITY
971 const key_equal& key_eq() const _NOEXCEPT {return __p3_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000972
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000973 _LIBCPP_INLINE_VISIBILITY
974 __node_allocator& __node_alloc() _NOEXCEPT {return __p1_.second();}
975 _LIBCPP_INLINE_VISIBILITY
976 const __node_allocator& __node_alloc() const _NOEXCEPT
977 {return __p1_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000978
979public:
980 typedef __hash_iterator<__node_pointer> iterator;
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +0000981 typedef __hash_const_iterator<__node_pointer> const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000982 typedef __hash_local_iterator<__node_pointer> local_iterator;
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +0000983 typedef __hash_const_local_iterator<__node_pointer> const_local_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000984
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000985 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000986 __hash_table()
987 _NOEXCEPT_(
988 is_nothrow_default_constructible<__bucket_list>::value &&
989 is_nothrow_default_constructible<__first_node>::value &&
990 is_nothrow_default_constructible<__node_allocator>::value &&
991 is_nothrow_default_constructible<hasher>::value &&
992 is_nothrow_default_constructible<key_equal>::value);
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000993 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000994 __hash_table(const hasher& __hf, const key_equal& __eql);
995 __hash_table(const hasher& __hf, const key_equal& __eql,
996 const allocator_type& __a);
997 explicit __hash_table(const allocator_type& __a);
998 __hash_table(const __hash_table& __u);
999 __hash_table(const __hash_table& __u, const allocator_type& __a);
Eric Fiselier2960ae22016-02-11 11:59:44 +00001000#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001001 __hash_table(__hash_table&& __u)
1002 _NOEXCEPT_(
1003 is_nothrow_move_constructible<__bucket_list>::value &&
1004 is_nothrow_move_constructible<__first_node>::value &&
1005 is_nothrow_move_constructible<__node_allocator>::value &&
1006 is_nothrow_move_constructible<hasher>::value &&
1007 is_nothrow_move_constructible<key_equal>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001008 __hash_table(__hash_table&& __u, const allocator_type& __a);
Eric Fiselier2960ae22016-02-11 11:59:44 +00001009#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001010 ~__hash_table();
1011
1012 __hash_table& operator=(const __hash_table& __u);
Eric Fiselier2960ae22016-02-11 11:59:44 +00001013#ifndef _LIBCPP_CXX03_LANG
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001014 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001015 __hash_table& operator=(__hash_table&& __u)
1016 _NOEXCEPT_(
1017 __node_traits::propagate_on_container_move_assignment::value &&
1018 is_nothrow_move_assignable<__node_allocator>::value &&
1019 is_nothrow_move_assignable<hasher>::value &&
1020 is_nothrow_move_assignable<key_equal>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001021#endif
1022 template <class _InputIterator>
1023 void __assign_unique(_InputIterator __first, _InputIterator __last);
1024 template <class _InputIterator>
1025 void __assign_multi(_InputIterator __first, _InputIterator __last);
1026
Howard Hinnant99acc502010-09-21 17:32:39 +00001027 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001028 size_type max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001029 {
1030 return allocator_traits<__pointer_allocator>::max_size(
1031 __bucket_list_.get_deleter().__alloc());
1032 }
1033
1034 pair<iterator, bool> __node_insert_unique(__node_pointer __nd);
1035 iterator __node_insert_multi(__node_pointer __nd);
1036 iterator __node_insert_multi(const_iterator __p,
1037 __node_pointer __nd);
1038
Eric Fiselier2960ae22016-02-11 11:59:44 +00001039#ifndef _LIBCPP_CXX03_LANG
1040 template <class _Key, class ..._Args>
1041 pair<iterator, bool> __emplace_unique_key_args(_Key const& __k, _Args&&... __args);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001042
Eric Fiselier2960ae22016-02-11 11:59:44 +00001043 template <class... _Args>
1044 pair<iterator, bool> __emplace_unique(_Args&&... __args);
1045 template <class... _Args>
1046 iterator __emplace_multi(_Args&&... __args);
1047 template <class... _Args>
1048 iterator __emplace_hint_multi(const_iterator __p, _Args&&... __args);
1049
1050
Eric Fiselierfdae69a2015-06-13 07:18:32 +00001051 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2960ae22016-02-11 11:59:44 +00001052 pair<iterator, bool>
1053 __insert_unique(__container_value_type&& __x) {
1054 return __emplace_unique_key_args(_NodeTypes::__get_key(__x), _VSTD::move(__x));
1055 }
1056
1057 template <class _Pp, class = typename enable_if<
1058 !__is_same_uncvref<_Pp, __container_value_type>::value
1059 >::type>
Eric Fiselierfdae69a2015-06-13 07:18:32 +00001060 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2960ae22016-02-11 11:59:44 +00001061 pair<iterator, bool> __insert_unique(_Pp&& __x) {
1062 return __emplace_unique(_VSTD::forward<_Pp>(__x));
1063 }
1064
1065 template <class _Pp>
1066 _LIBCPP_INLINE_VISIBILITY
1067 iterator __insert_multi(_Pp&& __x) {
1068 return __emplace_multi(_VSTD::forward<_Pp>(__x));
1069 }
1070
1071 template <class _Pp>
1072 _LIBCPP_INLINE_VISIBILITY
1073 iterator __insert_multi(const_iterator __p, _Pp&& __x) {
1074 return __emplace_hint_multi(__p, _VSTD::forward<_Pp>(__x));
1075 }
1076
1077#else // !defined(_LIBCPP_CXX03_LANG)
1078 template <class _Key, class _Args>
1079 pair<iterator, bool> __emplace_unique_key_args(_Key const&, _Args& __args);
1080
1081 iterator __insert_multi(const __container_value_type& __x);
1082 iterator __insert_multi(const_iterator __p, const __container_value_type& __x);
Eric Fiselierfdae69a2015-06-13 07:18:32 +00001083#endif
1084
Eric Fiselier2960ae22016-02-11 11:59:44 +00001085 _LIBCPP_INLINE_VISIBILITY
1086 pair<iterator, bool> __insert_unique(const __container_value_type& __x) {
1087 return __emplace_unique_key_args(_NodeTypes::__get_key(__x), __x);
1088 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001089
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001090 void clear() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001091 void rehash(size_type __n);
Howard Hinnant99acc502010-09-21 17:32:39 +00001092 _LIBCPP_INLINE_VISIBILITY void reserve(size_type __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001093 {rehash(static_cast<size_type>(ceil(__n / max_load_factor())));}
Howard Hinnant99acc502010-09-21 17:32:39 +00001094
1095 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001096 size_type bucket_count() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001097 {
1098 return __bucket_list_.get_deleter().size();
1099 }
1100
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001101 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001102 iterator begin() _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001103 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001104 iterator end() _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001105 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001106 const_iterator begin() const _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001107 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001108 const_iterator end() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001109
1110 template <class _Key>
Howard Hinnant99acc502010-09-21 17:32:39 +00001111 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001112 size_type bucket(const _Key& __k) const
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00001113 {
1114 _LIBCPP_ASSERT(bucket_count() > 0,
1115 "unordered container::bucket(key) called when bucket_count() == 0");
1116 return __constrain_hash(hash_function()(__k), bucket_count());
1117 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001118
1119 template <class _Key>
1120 iterator find(const _Key& __x);
1121 template <class _Key>
1122 const_iterator find(const _Key& __x) const;
1123
Howard Hinnant99968442011-11-29 18:15:50 +00001124 typedef __hash_node_destructor<__node_allocator> _Dp;
1125 typedef unique_ptr<__node, _Dp> __node_holder;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001126
1127 iterator erase(const_iterator __p);
1128 iterator erase(const_iterator __first, const_iterator __last);
1129 template <class _Key>
1130 size_type __erase_unique(const _Key& __k);
1131 template <class _Key>
1132 size_type __erase_multi(const _Key& __k);
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001133 __node_holder remove(const_iterator __p) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001134
1135 template <class _Key>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001136 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001137 size_type __count_unique(const _Key& __k) const;
1138 template <class _Key>
1139 size_type __count_multi(const _Key& __k) const;
1140
1141 template <class _Key>
1142 pair<iterator, iterator>
1143 __equal_range_unique(const _Key& __k);
1144 template <class _Key>
1145 pair<const_iterator, const_iterator>
1146 __equal_range_unique(const _Key& __k) const;
1147
1148 template <class _Key>
1149 pair<iterator, iterator>
1150 __equal_range_multi(const _Key& __k);
1151 template <class _Key>
1152 pair<const_iterator, const_iterator>
1153 __equal_range_multi(const _Key& __k) const;
1154
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001155 void swap(__hash_table& __u)
Eric Fiselier692177d2015-07-18 20:40:46 +00001156#if _LIBCPP_STD_VER <= 11
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001157 _NOEXCEPT_(
Marshall Clow7d914d12015-07-13 20:04:56 +00001158 __is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value
Marshall Clow7d914d12015-07-13 20:04:56 +00001159 && (!allocator_traits<__pointer_allocator>::propagate_on_container_swap::value
1160 || __is_nothrow_swappable<__pointer_allocator>::value)
1161 && (!__node_traits::propagate_on_container_swap::value
1162 || __is_nothrow_swappable<__node_allocator>::value)
Marshall Clow7d914d12015-07-13 20:04:56 +00001163 );
Eric Fiselier692177d2015-07-18 20:40:46 +00001164#else
1165 _NOEXCEPT_(__is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value);
1166#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001167
Howard Hinnant99acc502010-09-21 17:32:39 +00001168 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001169 size_type max_bucket_count() const _NOEXCEPT
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001170 {return __pointer_alloc_traits::max_size(__bucket_list_.get_deleter().__alloc());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001171 size_type bucket_size(size_type __n) const;
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001172 _LIBCPP_INLINE_VISIBILITY float load_factor() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001173 {
1174 size_type __bc = bucket_count();
1175 return __bc != 0 ? (float)size() / __bc : 0.f;
1176 }
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001177 _LIBCPP_INLINE_VISIBILITY void max_load_factor(float __mlf) _NOEXCEPT
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00001178 {
1179 _LIBCPP_ASSERT(__mlf > 0,
1180 "unordered container::max_load_factor(lf) called with lf <= 0");
1181 max_load_factor() = _VSTD::max(__mlf, load_factor());
1182 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001183
Howard Hinnant39213642013-07-23 22:01:58 +00001184 _LIBCPP_INLINE_VISIBILITY
1185 local_iterator
1186 begin(size_type __n)
1187 {
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00001188 _LIBCPP_ASSERT(__n < bucket_count(),
1189 "unordered container::begin(n) called with n >= bucket_count()");
Howard Hinnant39213642013-07-23 22:01:58 +00001190#if _LIBCPP_DEBUG_LEVEL >= 2
1191 return local_iterator(__bucket_list_[__n], __n, bucket_count(), this);
1192#else
1193 return local_iterator(__bucket_list_[__n], __n, bucket_count());
1194#endif
1195 }
1196
1197 _LIBCPP_INLINE_VISIBILITY
1198 local_iterator
1199 end(size_type __n)
1200 {
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00001201 _LIBCPP_ASSERT(__n < bucket_count(),
1202 "unordered container::end(n) called with n >= bucket_count()");
Howard Hinnant39213642013-07-23 22:01:58 +00001203#if _LIBCPP_DEBUG_LEVEL >= 2
1204 return local_iterator(nullptr, __n, bucket_count(), this);
1205#else
1206 return local_iterator(nullptr, __n, bucket_count());
1207#endif
1208 }
1209
1210 _LIBCPP_INLINE_VISIBILITY
1211 const_local_iterator
1212 cbegin(size_type __n) const
1213 {
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00001214 _LIBCPP_ASSERT(__n < bucket_count(),
1215 "unordered container::cbegin(n) called with n >= bucket_count()");
Howard Hinnant39213642013-07-23 22:01:58 +00001216#if _LIBCPP_DEBUG_LEVEL >= 2
1217 return const_local_iterator(__bucket_list_[__n], __n, bucket_count(), this);
1218#else
1219 return const_local_iterator(__bucket_list_[__n], __n, bucket_count());
1220#endif
1221 }
1222
1223 _LIBCPP_INLINE_VISIBILITY
1224 const_local_iterator
1225 cend(size_type __n) const
1226 {
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00001227 _LIBCPP_ASSERT(__n < bucket_count(),
1228 "unordered container::cend(n) called with n >= bucket_count()");
Howard Hinnant39213642013-07-23 22:01:58 +00001229#if _LIBCPP_DEBUG_LEVEL >= 2
1230 return const_local_iterator(nullptr, __n, bucket_count(), this);
1231#else
1232 return const_local_iterator(nullptr, __n, bucket_count());
1233#endif
1234 }
1235
1236#if _LIBCPP_DEBUG_LEVEL >= 2
1237
1238 bool __dereferenceable(const const_iterator* __i) const;
1239 bool __decrementable(const const_iterator* __i) const;
1240 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1241 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1242
1243#endif // _LIBCPP_DEBUG_LEVEL >= 2
1244
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001245private:
1246 void __rehash(size_type __n);
1247
Eric Fiselier2960ae22016-02-11 11:59:44 +00001248#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001249 template <class ..._Args>
Eric Fiselier2960ae22016-02-11 11:59:44 +00001250 __node_holder __construct_node(_Args&& ...__args);
1251
1252 template <class _First, class ..._Rest>
1253 __node_holder __construct_node_hash(size_t __hash, _First&& __f, _Rest&&... __rest);
1254#else // _LIBCPP_CXX03_LANG
1255 __node_holder __construct_node(const __container_value_type& __v);
1256 __node_holder __construct_node_hash(size_t __hash, const __container_value_type& __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001257#endif
Eric Fiselier2960ae22016-02-11 11:59:44 +00001258
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001259
Howard Hinnant99acc502010-09-21 17:32:39 +00001260 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001261 void __copy_assign_alloc(const __hash_table& __u)
1262 {__copy_assign_alloc(__u, integral_constant<bool,
1263 __node_traits::propagate_on_container_copy_assignment::value>());}
1264 void __copy_assign_alloc(const __hash_table& __u, true_type);
Howard Hinnant99acc502010-09-21 17:32:39 +00001265 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00001266 void __copy_assign_alloc(const __hash_table&, false_type) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001267
Eric Fiselier2960ae22016-02-11 11:59:44 +00001268#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001269 void __move_assign(__hash_table& __u, false_type);
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001270 void __move_assign(__hash_table& __u, true_type)
1271 _NOEXCEPT_(
1272 is_nothrow_move_assignable<__node_allocator>::value &&
1273 is_nothrow_move_assignable<hasher>::value &&
1274 is_nothrow_move_assignable<key_equal>::value);
1275 _LIBCPP_INLINE_VISIBILITY
1276 void __move_assign_alloc(__hash_table& __u)
1277 _NOEXCEPT_(
1278 !__node_traits::propagate_on_container_move_assignment::value ||
1279 (is_nothrow_move_assignable<__pointer_allocator>::value &&
1280 is_nothrow_move_assignable<__node_allocator>::value))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001281 {__move_assign_alloc(__u, integral_constant<bool,
1282 __node_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnant99acc502010-09-21 17:32:39 +00001283 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001284 void __move_assign_alloc(__hash_table& __u, true_type)
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001285 _NOEXCEPT_(
1286 is_nothrow_move_assignable<__pointer_allocator>::value &&
1287 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001288 {
1289 __bucket_list_.get_deleter().__alloc() =
Howard Hinnant0949eed2011-06-30 21:18:19 +00001290 _VSTD::move(__u.__bucket_list_.get_deleter().__alloc());
1291 __node_alloc() = _VSTD::move(__u.__node_alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001292 }
Howard Hinnant99acc502010-09-21 17:32:39 +00001293 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001294 void __move_assign_alloc(__hash_table&, false_type) _NOEXCEPT {}
Eric Fiselier2960ae22016-02-11 11:59:44 +00001295#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001296
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001297 void __deallocate(__node_pointer __np) _NOEXCEPT;
1298 __node_pointer __detach() _NOEXCEPT;
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001299
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001300 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_map;
1301 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_multimap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001302};
1303
1304template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001305inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001306__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table()
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001307 _NOEXCEPT_(
1308 is_nothrow_default_constructible<__bucket_list>::value &&
1309 is_nothrow_default_constructible<__first_node>::value &&
Eric Fiselierc8f54c22015-12-16 00:53:04 +00001310 is_nothrow_default_constructible<__node_allocator>::value &&
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001311 is_nothrow_default_constructible<hasher>::value &&
1312 is_nothrow_default_constructible<key_equal>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001313 : __p2_(0),
1314 __p3_(1.0f)
1315{
1316}
1317
1318template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001319inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001320__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const hasher& __hf,
1321 const key_equal& __eql)
1322 : __bucket_list_(nullptr, __bucket_list_deleter()),
1323 __p1_(),
1324 __p2_(0, __hf),
1325 __p3_(1.0f, __eql)
1326{
1327}
1328
1329template <class _Tp, class _Hash, class _Equal, class _Alloc>
1330__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const hasher& __hf,
1331 const key_equal& __eql,
1332 const allocator_type& __a)
1333 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1334 __p1_(__node_allocator(__a)),
1335 __p2_(0, __hf),
1336 __p3_(1.0f, __eql)
1337{
1338}
1339
1340template <class _Tp, class _Hash, class _Equal, class _Alloc>
1341__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const allocator_type& __a)
1342 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1343 __p1_(__node_allocator(__a)),
1344 __p2_(0),
1345 __p3_(1.0f)
1346{
1347}
1348
1349template <class _Tp, class _Hash, class _Equal, class _Alloc>
1350__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const __hash_table& __u)
1351 : __bucket_list_(nullptr,
1352 __bucket_list_deleter(allocator_traits<__pointer_allocator>::
1353 select_on_container_copy_construction(
1354 __u.__bucket_list_.get_deleter().__alloc()), 0)),
1355 __p1_(allocator_traits<__node_allocator>::
1356 select_on_container_copy_construction(__u.__node_alloc())),
1357 __p2_(0, __u.hash_function()),
1358 __p3_(__u.__p3_)
1359{
1360}
1361
1362template <class _Tp, class _Hash, class _Equal, class _Alloc>
1363__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const __hash_table& __u,
1364 const allocator_type& __a)
1365 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1366 __p1_(__node_allocator(__a)),
1367 __p2_(0, __u.hash_function()),
1368 __p3_(__u.__p3_)
1369{
1370}
1371
Eric Fiselier2960ae22016-02-11 11:59:44 +00001372#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001373
1374template <class _Tp, class _Hash, class _Equal, class _Alloc>
1375__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(__hash_table&& __u)
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001376 _NOEXCEPT_(
1377 is_nothrow_move_constructible<__bucket_list>::value &&
1378 is_nothrow_move_constructible<__first_node>::value &&
Eric Fiselierc8f54c22015-12-16 00:53:04 +00001379 is_nothrow_move_constructible<__node_allocator>::value &&
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001380 is_nothrow_move_constructible<hasher>::value &&
1381 is_nothrow_move_constructible<key_equal>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001382 : __bucket_list_(_VSTD::move(__u.__bucket_list_)),
1383 __p1_(_VSTD::move(__u.__p1_)),
1384 __p2_(_VSTD::move(__u.__p2_)),
1385 __p3_(_VSTD::move(__u.__p3_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001386{
1387 if (size() > 0)
1388 {
Howard Hinnant7a445152012-07-06 17:31:14 +00001389 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash_, bucket_count())] =
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001390 static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001391 __u.__p1_.first().__next_ = nullptr;
1392 __u.size() = 0;
1393 }
1394}
1395
1396template <class _Tp, class _Hash, class _Equal, class _Alloc>
1397__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(__hash_table&& __u,
1398 const allocator_type& __a)
1399 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1400 __p1_(__node_allocator(__a)),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001401 __p2_(0, _VSTD::move(__u.hash_function())),
1402 __p3_(_VSTD::move(__u.__p3_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001403{
1404 if (__a == allocator_type(__u.__node_alloc()))
1405 {
1406 __bucket_list_.reset(__u.__bucket_list_.release());
1407 __bucket_list_.get_deleter().size() = __u.__bucket_list_.get_deleter().size();
1408 __u.__bucket_list_.get_deleter().size() = 0;
1409 if (__u.size() > 0)
1410 {
1411 __p1_.first().__next_ = __u.__p1_.first().__next_;
1412 __u.__p1_.first().__next_ = nullptr;
Howard Hinnant7a445152012-07-06 17:31:14 +00001413 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash_, bucket_count())] =
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001414 static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001415 size() = __u.size();
1416 __u.size() = 0;
1417 }
1418 }
1419}
1420
Eric Fiselier2960ae22016-02-11 11:59:44 +00001421#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001422
1423template <class _Tp, class _Hash, class _Equal, class _Alloc>
1424__hash_table<_Tp, _Hash, _Equal, _Alloc>::~__hash_table()
1425{
1426 __deallocate(__p1_.first().__next_);
Howard Hinnant39213642013-07-23 22:01:58 +00001427#if _LIBCPP_DEBUG_LEVEL >= 2
1428 __get_db()->__erase_c(this);
1429#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001430}
1431
1432template <class _Tp, class _Hash, class _Equal, class _Alloc>
1433void
1434__hash_table<_Tp, _Hash, _Equal, _Alloc>::__copy_assign_alloc(
1435 const __hash_table& __u, true_type)
1436{
1437 if (__node_alloc() != __u.__node_alloc())
1438 {
1439 clear();
1440 __bucket_list_.reset();
1441 __bucket_list_.get_deleter().size() = 0;
1442 }
1443 __bucket_list_.get_deleter().__alloc() = __u.__bucket_list_.get_deleter().__alloc();
1444 __node_alloc() = __u.__node_alloc();
1445}
1446
1447template <class _Tp, class _Hash, class _Equal, class _Alloc>
1448__hash_table<_Tp, _Hash, _Equal, _Alloc>&
1449__hash_table<_Tp, _Hash, _Equal, _Alloc>::operator=(const __hash_table& __u)
1450{
1451 if (this != &__u)
1452 {
1453 __copy_assign_alloc(__u);
1454 hash_function() = __u.hash_function();
1455 key_eq() = __u.key_eq();
1456 max_load_factor() = __u.max_load_factor();
1457 __assign_multi(__u.begin(), __u.end());
1458 }
1459 return *this;
1460}
1461
1462template <class _Tp, class _Hash, class _Equal, class _Alloc>
1463void
1464__hash_table<_Tp, _Hash, _Equal, _Alloc>::__deallocate(__node_pointer __np)
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001465 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001466{
1467 __node_allocator& __na = __node_alloc();
1468 while (__np != nullptr)
1469 {
1470 __node_pointer __next = __np->__next_;
Howard Hinnant39213642013-07-23 22:01:58 +00001471#if _LIBCPP_DEBUG_LEVEL >= 2
1472 __c_node* __c = __get_db()->__find_c_and_lock(this);
1473 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1474 {
1475 --__p;
1476 iterator* __i = static_cast<iterator*>((*__p)->__i_);
1477 if (__i->__node_ == __np)
1478 {
1479 (*__p)->__c_ = nullptr;
1480 if (--__c->end_ != __p)
1481 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1482 }
1483 }
1484 __get_db()->unlock();
1485#endif
Eric Fiselier2960ae22016-02-11 11:59:44 +00001486 __node_traits::destroy(__na, _NodeTypes::__get_ptr(__np->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001487 __node_traits::deallocate(__na, __np, 1);
1488 __np = __next;
1489 }
1490}
1491
1492template <class _Tp, class _Hash, class _Equal, class _Alloc>
1493typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_pointer
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001494__hash_table<_Tp, _Hash, _Equal, _Alloc>::__detach() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001495{
1496 size_type __bc = bucket_count();
1497 for (size_type __i = 0; __i < __bc; ++__i)
1498 __bucket_list_[__i] = nullptr;
1499 size() = 0;
1500 __node_pointer __cache = __p1_.first().__next_;
1501 __p1_.first().__next_ = nullptr;
1502 return __cache;
1503}
1504
Eric Fiselier2960ae22016-02-11 11:59:44 +00001505#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001506
1507template <class _Tp, class _Hash, class _Equal, class _Alloc>
1508void
1509__hash_table<_Tp, _Hash, _Equal, _Alloc>::__move_assign(
1510 __hash_table& __u, true_type)
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001511 _NOEXCEPT_(
1512 is_nothrow_move_assignable<__node_allocator>::value &&
1513 is_nothrow_move_assignable<hasher>::value &&
1514 is_nothrow_move_assignable<key_equal>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001515{
1516 clear();
1517 __bucket_list_.reset(__u.__bucket_list_.release());
1518 __bucket_list_.get_deleter().size() = __u.__bucket_list_.get_deleter().size();
1519 __u.__bucket_list_.get_deleter().size() = 0;
1520 __move_assign_alloc(__u);
1521 size() = __u.size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001522 hash_function() = _VSTD::move(__u.hash_function());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001523 max_load_factor() = __u.max_load_factor();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001524 key_eq() = _VSTD::move(__u.key_eq());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001525 __p1_.first().__next_ = __u.__p1_.first().__next_;
1526 if (size() > 0)
1527 {
Howard Hinnant7a445152012-07-06 17:31:14 +00001528 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash_, bucket_count())] =
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001529 static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001530 __u.__p1_.first().__next_ = nullptr;
1531 __u.size() = 0;
1532 }
Howard Hinnant39213642013-07-23 22:01:58 +00001533#if _LIBCPP_DEBUG_LEVEL >= 2
1534 __get_db()->swap(this, &__u);
1535#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001536}
1537
1538template <class _Tp, class _Hash, class _Equal, class _Alloc>
1539void
1540__hash_table<_Tp, _Hash, _Equal, _Alloc>::__move_assign(
1541 __hash_table& __u, false_type)
1542{
1543 if (__node_alloc() == __u.__node_alloc())
1544 __move_assign(__u, true_type());
1545 else
1546 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001547 hash_function() = _VSTD::move(__u.hash_function());
1548 key_eq() = _VSTD::move(__u.key_eq());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001549 max_load_factor() = __u.max_load_factor();
1550 if (bucket_count() != 0)
1551 {
1552 __node_pointer __cache = __detach();
1553#ifndef _LIBCPP_NO_EXCEPTIONS
1554 try
1555 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001556#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001557 const_iterator __i = __u.begin();
1558 while (__cache != nullptr && __u.size() != 0)
1559 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001560 __cache->__value_ = _VSTD::move(__u.remove(__i++)->__value_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001561 __node_pointer __next = __cache->__next_;
1562 __node_insert_multi(__cache);
1563 __cache = __next;
1564 }
1565#ifndef _LIBCPP_NO_EXCEPTIONS
1566 }
1567 catch (...)
1568 {
1569 __deallocate(__cache);
1570 throw;
1571 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001572#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001573 __deallocate(__cache);
1574 }
1575 const_iterator __i = __u.begin();
1576 while (__u.size() != 0)
1577 {
Eric Fiselier2960ae22016-02-11 11:59:44 +00001578 __node_holder __h = __construct_node(_NodeTypes::__move(__u.remove(__i++)->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001579 __node_insert_multi(__h.get());
1580 __h.release();
1581 }
1582 }
1583}
1584
1585template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001586inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001587__hash_table<_Tp, _Hash, _Equal, _Alloc>&
1588__hash_table<_Tp, _Hash, _Equal, _Alloc>::operator=(__hash_table&& __u)
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001589 _NOEXCEPT_(
1590 __node_traits::propagate_on_container_move_assignment::value &&
1591 is_nothrow_move_assignable<__node_allocator>::value &&
1592 is_nothrow_move_assignable<hasher>::value &&
1593 is_nothrow_move_assignable<key_equal>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001594{
1595 __move_assign(__u, integral_constant<bool,
1596 __node_traits::propagate_on_container_move_assignment::value>());
1597 return *this;
1598}
1599
Eric Fiselier2960ae22016-02-11 11:59:44 +00001600#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001601
1602template <class _Tp, class _Hash, class _Equal, class _Alloc>
1603template <class _InputIterator>
1604void
1605__hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_unique(_InputIterator __first,
1606 _InputIterator __last)
1607{
Eric Fiselier2960ae22016-02-11 11:59:44 +00001608 typedef iterator_traits<_InputIterator> _ITraits;
1609 typedef typename _ITraits::value_type _ItValueType;
1610 static_assert((is_same<_ItValueType, __container_value_type>::value),
1611 "__assign_unique may only be called with the containers value type");
1612
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001613 if (bucket_count() != 0)
1614 {
1615 __node_pointer __cache = __detach();
1616#ifndef _LIBCPP_NO_EXCEPTIONS
1617 try
1618 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001619#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001620 for (; __cache != nullptr && __first != __last; ++__first)
1621 {
1622 __cache->__value_ = *__first;
1623 __node_pointer __next = __cache->__next_;
1624 __node_insert_unique(__cache);
1625 __cache = __next;
1626 }
1627#ifndef _LIBCPP_NO_EXCEPTIONS
1628 }
1629 catch (...)
1630 {
1631 __deallocate(__cache);
1632 throw;
1633 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001634#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001635 __deallocate(__cache);
1636 }
1637 for (; __first != __last; ++__first)
1638 __insert_unique(*__first);
1639}
1640
1641template <class _Tp, class _Hash, class _Equal, class _Alloc>
1642template <class _InputIterator>
1643void
1644__hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_multi(_InputIterator __first,
1645 _InputIterator __last)
1646{
Eric Fiselier2960ae22016-02-11 11:59:44 +00001647 typedef iterator_traits<_InputIterator> _ITraits;
1648 typedef typename _ITraits::value_type _ItValueType;
1649 static_assert((is_same<_ItValueType, __container_value_type>::value ||
1650 is_same<_ItValueType, __node_value_type>::value),
1651 "__assign_multi may only be called with the containers value type"
1652 " or the nodes value type");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001653 if (bucket_count() != 0)
1654 {
1655 __node_pointer __cache = __detach();
1656#ifndef _LIBCPP_NO_EXCEPTIONS
1657 try
1658 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001659#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001660 for (; __cache != nullptr && __first != __last; ++__first)
1661 {
1662 __cache->__value_ = *__first;
1663 __node_pointer __next = __cache->__next_;
1664 __node_insert_multi(__cache);
1665 __cache = __next;
1666 }
1667#ifndef _LIBCPP_NO_EXCEPTIONS
1668 }
1669 catch (...)
1670 {
1671 __deallocate(__cache);
1672 throw;
1673 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001674#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001675 __deallocate(__cache);
1676 }
1677 for (; __first != __last; ++__first)
Eric Fiselier2960ae22016-02-11 11:59:44 +00001678 __insert_multi(_NodeTypes::__get_value(*__first));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001679}
1680
1681template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001682inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001683typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001684__hash_table<_Tp, _Hash, _Equal, _Alloc>::begin() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001685{
Howard Hinnant39213642013-07-23 22:01:58 +00001686#if _LIBCPP_DEBUG_LEVEL >= 2
1687 return iterator(__p1_.first().__next_, this);
1688#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001689 return iterator(__p1_.first().__next_);
Howard Hinnant39213642013-07-23 22:01:58 +00001690#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001691}
1692
1693template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001694inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001695typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001696__hash_table<_Tp, _Hash, _Equal, _Alloc>::end() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001697{
Howard Hinnant39213642013-07-23 22:01:58 +00001698#if _LIBCPP_DEBUG_LEVEL >= 2
1699 return iterator(nullptr, this);
1700#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001701 return iterator(nullptr);
Howard Hinnant39213642013-07-23 22:01:58 +00001702#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001703}
1704
1705template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001706inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001707typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001708__hash_table<_Tp, _Hash, _Equal, _Alloc>::begin() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001709{
Howard Hinnant39213642013-07-23 22:01:58 +00001710#if _LIBCPP_DEBUG_LEVEL >= 2
1711 return const_iterator(__p1_.first().__next_, this);
1712#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001713 return const_iterator(__p1_.first().__next_);
Howard Hinnant39213642013-07-23 22:01:58 +00001714#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001715}
1716
1717template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001718inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001719typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001720__hash_table<_Tp, _Hash, _Equal, _Alloc>::end() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001721{
Howard Hinnant39213642013-07-23 22:01:58 +00001722#if _LIBCPP_DEBUG_LEVEL >= 2
1723 return const_iterator(nullptr, this);
1724#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001725 return const_iterator(nullptr);
Howard Hinnant39213642013-07-23 22:01:58 +00001726#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001727}
1728
1729template <class _Tp, class _Hash, class _Equal, class _Alloc>
1730void
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001731__hash_table<_Tp, _Hash, _Equal, _Alloc>::clear() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001732{
1733 if (size() > 0)
1734 {
1735 __deallocate(__p1_.first().__next_);
1736 __p1_.first().__next_ = nullptr;
1737 size_type __bc = bucket_count();
Howard Hinnant9f66bff2011-07-05 14:14:17 +00001738 for (size_type __i = 0; __i < __bc; ++__i)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001739 __bucket_list_[__i] = nullptr;
1740 size() = 0;
1741 }
1742}
1743
1744template <class _Tp, class _Hash, class _Equal, class _Alloc>
1745pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
1746__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_unique(__node_pointer __nd)
1747{
1748 __nd->__hash_ = hash_function()(__nd->__value_);
1749 size_type __bc = bucket_count();
1750 bool __inserted = false;
1751 __node_pointer __ndptr;
1752 size_t __chash;
1753 if (__bc != 0)
1754 {
Howard Hinnant7a445152012-07-06 17:31:14 +00001755 __chash = __constrain_hash(__nd->__hash_, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001756 __ndptr = __bucket_list_[__chash];
1757 if (__ndptr != nullptr)
1758 {
1759 for (__ndptr = __ndptr->__next_; __ndptr != nullptr &&
Howard Hinnant7a445152012-07-06 17:31:14 +00001760 __constrain_hash(__ndptr->__hash_, __bc) == __chash;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001761 __ndptr = __ndptr->__next_)
1762 {
1763 if (key_eq()(__ndptr->__value_, __nd->__value_))
1764 goto __done;
1765 }
1766 }
1767 }
1768 {
1769 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1770 {
Eric Fiselier57947ca2015-02-02 21:31:48 +00001771 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001772 size_type(ceil(float(size() + 1) / max_load_factor()))));
1773 __bc = bucket_count();
Howard Hinnant7a445152012-07-06 17:31:14 +00001774 __chash = __constrain_hash(__nd->__hash_, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001775 }
1776 // insert_after __bucket_list_[__chash], or __first_node if bucket is null
1777 __node_pointer __pn = __bucket_list_[__chash];
1778 if (__pn == nullptr)
1779 {
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001780 __pn = static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001781 __nd->__next_ = __pn->__next_;
1782 __pn->__next_ = __nd;
1783 // fix up __bucket_list_
1784 __bucket_list_[__chash] = __pn;
1785 if (__nd->__next_ != nullptr)
Howard Hinnant7a445152012-07-06 17:31:14 +00001786 __bucket_list_[__constrain_hash(__nd->__next_->__hash_, __bc)] = __nd;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001787 }
1788 else
1789 {
1790 __nd->__next_ = __pn->__next_;
1791 __pn->__next_ = __nd;
1792 }
1793 __ndptr = __nd;
1794 // increment size
1795 ++size();
1796 __inserted = true;
1797 }
1798__done:
Howard Hinnant39213642013-07-23 22:01:58 +00001799#if _LIBCPP_DEBUG_LEVEL >= 2
1800 return pair<iterator, bool>(iterator(__ndptr, this), __inserted);
1801#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001802 return pair<iterator, bool>(iterator(__ndptr), __inserted);
Howard Hinnant39213642013-07-23 22:01:58 +00001803#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001804}
1805
1806template <class _Tp, class _Hash, class _Equal, class _Alloc>
1807typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1808__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi(__node_pointer __cp)
1809{
1810 __cp->__hash_ = hash_function()(__cp->__value_);
1811 size_type __bc = bucket_count();
1812 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1813 {
Eric Fiselier57947ca2015-02-02 21:31:48 +00001814 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001815 size_type(ceil(float(size() + 1) / max_load_factor()))));
1816 __bc = bucket_count();
1817 }
Howard Hinnant7a445152012-07-06 17:31:14 +00001818 size_t __chash = __constrain_hash(__cp->__hash_, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001819 __node_pointer __pn = __bucket_list_[__chash];
1820 if (__pn == nullptr)
1821 {
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001822 __pn = static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001823 __cp->__next_ = __pn->__next_;
1824 __pn->__next_ = __cp;
1825 // fix up __bucket_list_
1826 __bucket_list_[__chash] = __pn;
1827 if (__cp->__next_ != nullptr)
Howard Hinnant7a445152012-07-06 17:31:14 +00001828 __bucket_list_[__constrain_hash(__cp->__next_->__hash_, __bc)] = __cp;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001829 }
1830 else
1831 {
1832 for (bool __found = false; __pn->__next_ != nullptr &&
Howard Hinnant7a445152012-07-06 17:31:14 +00001833 __constrain_hash(__pn->__next_->__hash_, __bc) == __chash;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001834 __pn = __pn->__next_)
Howard Hinnant324bb032010-08-22 00:02:43 +00001835 {
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001836 // __found key_eq() action
1837 // false false loop
1838 // true true loop
1839 // false true set __found to true
1840 // true false break
1841 if (__found != (__pn->__next_->__hash_ == __cp->__hash_ &&
1842 key_eq()(__pn->__next_->__value_, __cp->__value_)))
1843 {
1844 if (!__found)
1845 __found = true;
1846 else
1847 break;
1848 }
1849 }
1850 __cp->__next_ = __pn->__next_;
1851 __pn->__next_ = __cp;
1852 if (__cp->__next_ != nullptr)
1853 {
Howard Hinnant7a445152012-07-06 17:31:14 +00001854 size_t __nhash = __constrain_hash(__cp->__next_->__hash_, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001855 if (__nhash != __chash)
1856 __bucket_list_[__nhash] = __cp;
1857 }
1858 }
1859 ++size();
Howard Hinnant39213642013-07-23 22:01:58 +00001860#if _LIBCPP_DEBUG_LEVEL >= 2
1861 return iterator(__cp, this);
1862#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001863 return iterator(__cp);
Howard Hinnant39213642013-07-23 22:01:58 +00001864#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001865}
1866
1867template <class _Tp, class _Hash, class _Equal, class _Alloc>
1868typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1869__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi(
1870 const_iterator __p, __node_pointer __cp)
1871{
Howard Hinnant824c1992013-08-02 17:50:49 +00001872#if _LIBCPP_DEBUG_LEVEL >= 2
1873 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1874 "unordered container::emplace_hint(const_iterator, args...) called with an iterator not"
1875 " referring to this unordered container");
1876#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001877 if (__p != end() && key_eq()(*__p, __cp->__value_))
1878 {
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001879 __node_pointer __np = __p.__node_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001880 __cp->__hash_ = __np->__hash_;
1881 size_type __bc = bucket_count();
1882 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1883 {
Eric Fiselier57947ca2015-02-02 21:31:48 +00001884 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001885 size_type(ceil(float(size() + 1) / max_load_factor()))));
1886 __bc = bucket_count();
1887 }
Howard Hinnant7a445152012-07-06 17:31:14 +00001888 size_t __chash = __constrain_hash(__cp->__hash_, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001889 __node_pointer __pp = __bucket_list_[__chash];
1890 while (__pp->__next_ != __np)
1891 __pp = __pp->__next_;
1892 __cp->__next_ = __np;
1893 __pp->__next_ = __cp;
1894 ++size();
Howard Hinnant39213642013-07-23 22:01:58 +00001895#if _LIBCPP_DEBUG_LEVEL >= 2
1896 return iterator(__cp, this);
1897#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001898 return iterator(__cp);
Howard Hinnant39213642013-07-23 22:01:58 +00001899#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001900 }
1901 return __node_insert_multi(__cp);
1902}
1903
Eric Fiselierfdae69a2015-06-13 07:18:32 +00001904
1905
Eric Fiselier2960ae22016-02-11 11:59:44 +00001906#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierfdae69a2015-06-13 07:18:32 +00001907template <class _Tp, class _Hash, class _Equal, class _Alloc>
Eric Fiselier2960ae22016-02-11 11:59:44 +00001908template <class _Key, class ..._Args>
Eric Fiselierfdae69a2015-06-13 07:18:32 +00001909_LIBCPP_INLINE_VISIBILITY
1910pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
Eric Fiselier2960ae22016-02-11 11:59:44 +00001911__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_key_args(_Key const& __k, _Args&&... __args)
Eric Fiselierfdae69a2015-06-13 07:18:32 +00001912#else
1913template <class _Tp, class _Hash, class _Equal, class _Alloc>
Eric Fiselier2960ae22016-02-11 11:59:44 +00001914template <class _Key, class _Args>
Eric Fiselierfdae69a2015-06-13 07:18:32 +00001915_LIBCPP_INLINE_VISIBILITY
1916pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
Eric Fiselier2960ae22016-02-11 11:59:44 +00001917__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_key_args(_Key const& __k, _Args& __args)
Eric Fiselierfdae69a2015-06-13 07:18:32 +00001918#endif
1919{
Eric Fiselier2960ae22016-02-11 11:59:44 +00001920
1921 size_t __hash = hash_function()(__k);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001922 size_type __bc = bucket_count();
1923 bool __inserted = false;
1924 __node_pointer __nd;
1925 size_t __chash;
1926 if (__bc != 0)
1927 {
Howard Hinnant7a445152012-07-06 17:31:14 +00001928 __chash = __constrain_hash(__hash, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001929 __nd = __bucket_list_[__chash];
1930 if (__nd != nullptr)
1931 {
1932 for (__nd = __nd->__next_; __nd != nullptr &&
Howard Hinnant7a445152012-07-06 17:31:14 +00001933 __constrain_hash(__nd->__hash_, __bc) == __chash;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001934 __nd = __nd->__next_)
1935 {
Eric Fiselier2960ae22016-02-11 11:59:44 +00001936 if (key_eq()(__nd->__value_, __k))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001937 goto __done;
1938 }
1939 }
1940 }
1941 {
Eric Fiselier2960ae22016-02-11 11:59:44 +00001942#ifndef _LIBCPP_CXX03_LANG
1943 __node_holder __h = __construct_node_hash(__hash, _VSTD::forward<_Args>(__args)...);
1944#else
1945 __node_holder __h = __construct_node_hash(__hash, __args);
1946#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001947 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1948 {
Eric Fiselier57947ca2015-02-02 21:31:48 +00001949 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001950 size_type(ceil(float(size() + 1) / max_load_factor()))));
1951 __bc = bucket_count();
Howard Hinnant7a445152012-07-06 17:31:14 +00001952 __chash = __constrain_hash(__hash, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001953 }
1954 // insert_after __bucket_list_[__chash], or __first_node if bucket is null
1955 __node_pointer __pn = __bucket_list_[__chash];
1956 if (__pn == nullptr)
1957 {
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001958 __pn = static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001959 __h->__next_ = __pn->__next_;
1960 __pn->__next_ = __h.get();
1961 // fix up __bucket_list_
1962 __bucket_list_[__chash] = __pn;
1963 if (__h->__next_ != nullptr)
Howard Hinnant7a445152012-07-06 17:31:14 +00001964 __bucket_list_[__constrain_hash(__h->__next_->__hash_, __bc)] = __h.get();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001965 }
1966 else
1967 {
1968 __h->__next_ = __pn->__next_;
1969 __pn->__next_ = __h.get();
1970 }
1971 __nd = __h.release();
1972 // increment size
1973 ++size();
1974 __inserted = true;
1975 }
1976__done:
Howard Hinnant39213642013-07-23 22:01:58 +00001977#if _LIBCPP_DEBUG_LEVEL >= 2
1978 return pair<iterator, bool>(iterator(__nd, this), __inserted);
1979#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001980 return pair<iterator, bool>(iterator(__nd), __inserted);
Howard Hinnant39213642013-07-23 22:01:58 +00001981#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001982}
1983
Eric Fiselier2960ae22016-02-11 11:59:44 +00001984#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001985
1986template <class _Tp, class _Hash, class _Equal, class _Alloc>
1987template <class... _Args>
1988pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
1989__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique(_Args&&... __args)
1990{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001991 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001992 pair<iterator, bool> __r = __node_insert_unique(__h.get());
1993 if (__r.second)
1994 __h.release();
1995 return __r;
1996}
1997
1998template <class _Tp, class _Hash, class _Equal, class _Alloc>
1999template <class... _Args>
2000typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2001__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_multi(_Args&&... __args)
2002{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002003 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002004 iterator __r = __node_insert_multi(__h.get());
2005 __h.release();
2006 return __r;
2007}
2008
2009template <class _Tp, class _Hash, class _Equal, class _Alloc>
2010template <class... _Args>
2011typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2012__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_hint_multi(
2013 const_iterator __p, _Args&&... __args)
2014{
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00002015#if _LIBCPP_DEBUG_LEVEL >= 2
2016 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2017 "unordered container::emplace_hint(const_iterator, args...) called with an iterator not"
2018 " referring to this unordered container");
2019#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00002020 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002021 iterator __r = __node_insert_multi(__p, __h.get());
2022 __h.release();
2023 return __r;
2024}
2025
Eric Fiselier2960ae22016-02-11 11:59:44 +00002026#else // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002027
2028template <class _Tp, class _Hash, class _Equal, class _Alloc>
2029typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
Eric Fiselier2960ae22016-02-11 11:59:44 +00002030__hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const __container_value_type& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002031{
2032 __node_holder __h = __construct_node(__x);
2033 iterator __r = __node_insert_multi(__h.get());
2034 __h.release();
2035 return __r;
2036}
2037
2038template <class _Tp, class _Hash, class _Equal, class _Alloc>
2039typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2040__hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const_iterator __p,
Eric Fiselier2960ae22016-02-11 11:59:44 +00002041 const __container_value_type& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002042{
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00002043#if _LIBCPP_DEBUG_LEVEL >= 2
2044 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2045 "unordered container::insert(const_iterator, lvalue) called with an iterator not"
2046 " referring to this unordered container");
2047#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002048 __node_holder __h = __construct_node(__x);
2049 iterator __r = __node_insert_multi(__p, __h.get());
2050 __h.release();
2051 return __r;
2052}
2053
Eric Fiselier2960ae22016-02-11 11:59:44 +00002054#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002055
2056template <class _Tp, class _Hash, class _Equal, class _Alloc>
2057void
2058__hash_table<_Tp, _Hash, _Equal, _Alloc>::rehash(size_type __n)
2059{
Howard Hinnant7a445152012-07-06 17:31:14 +00002060 if (__n == 1)
2061 __n = 2;
2062 else if (__n & (__n - 1))
2063 __n = __next_prime(__n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002064 size_type __bc = bucket_count();
2065 if (__n > __bc)
2066 __rehash(__n);
Howard Hinnant7a445152012-07-06 17:31:14 +00002067 else if (__n < __bc)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002068 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002069 __n = _VSTD::max<size_type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002070 (
2071 __n,
Eric Fiselier57947ca2015-02-02 21:31:48 +00002072 __is_hash_power2(__bc) ? __next_hash_pow2(size_t(ceil(float(size()) / max_load_factor()))) :
2073 __next_prime(size_t(ceil(float(size()) / max_load_factor())))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002074 );
2075 if (__n < __bc)
2076 __rehash(__n);
2077 }
2078}
2079
2080template <class _Tp, class _Hash, class _Equal, class _Alloc>
2081void
2082__hash_table<_Tp, _Hash, _Equal, _Alloc>::__rehash(size_type __nbc)
2083{
Howard Hinnant39213642013-07-23 22:01:58 +00002084#if _LIBCPP_DEBUG_LEVEL >= 2
2085 __get_db()->__invalidate_all(this);
2086#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002087 __pointer_allocator& __npa = __bucket_list_.get_deleter().__alloc();
2088 __bucket_list_.reset(__nbc > 0 ?
2089 __pointer_alloc_traits::allocate(__npa, __nbc) : nullptr);
2090 __bucket_list_.get_deleter().size() = __nbc;
2091 if (__nbc > 0)
2092 {
2093 for (size_type __i = 0; __i < __nbc; ++__i)
2094 __bucket_list_[__i] = nullptr;
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00002095 __node_pointer __pp(static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first())));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002096 __node_pointer __cp = __pp->__next_;
2097 if (__cp != nullptr)
2098 {
Howard Hinnant7a445152012-07-06 17:31:14 +00002099 size_type __chash = __constrain_hash(__cp->__hash_, __nbc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002100 __bucket_list_[__chash] = __pp;
2101 size_type __phash = __chash;
2102 for (__pp = __cp, __cp = __cp->__next_; __cp != nullptr;
2103 __cp = __pp->__next_)
2104 {
Howard Hinnant7a445152012-07-06 17:31:14 +00002105 __chash = __constrain_hash(__cp->__hash_, __nbc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002106 if (__chash == __phash)
2107 __pp = __cp;
2108 else
2109 {
2110 if (__bucket_list_[__chash] == nullptr)
2111 {
2112 __bucket_list_[__chash] = __pp;
2113 __pp = __cp;
2114 __phash = __chash;
2115 }
2116 else
2117 {
2118 __node_pointer __np = __cp;
2119 for (; __np->__next_ != nullptr &&
2120 key_eq()(__cp->__value_, __np->__next_->__value_);
2121 __np = __np->__next_)
2122 ;
2123 __pp->__next_ = __np->__next_;
2124 __np->__next_ = __bucket_list_[__chash]->__next_;
2125 __bucket_list_[__chash]->__next_ = __cp;
Howard Hinnant324bb032010-08-22 00:02:43 +00002126
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002127 }
2128 }
2129 }
2130 }
2131 }
2132}
2133
2134template <class _Tp, class _Hash, class _Equal, class _Alloc>
2135template <class _Key>
2136typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2137__hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k)
2138{
2139 size_t __hash = hash_function()(__k);
2140 size_type __bc = bucket_count();
2141 if (__bc != 0)
2142 {
Howard Hinnant7a445152012-07-06 17:31:14 +00002143 size_t __chash = __constrain_hash(__hash, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002144 __node_pointer __nd = __bucket_list_[__chash];
2145 if (__nd != nullptr)
2146 {
2147 for (__nd = __nd->__next_; __nd != nullptr &&
Howard Hinnant7a445152012-07-06 17:31:14 +00002148 __constrain_hash(__nd->__hash_, __bc) == __chash;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002149 __nd = __nd->__next_)
2150 {
2151 if (key_eq()(__nd->__value_, __k))
Howard Hinnant39213642013-07-23 22:01:58 +00002152#if _LIBCPP_DEBUG_LEVEL >= 2
2153 return iterator(__nd, this);
2154#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002155 return iterator(__nd);
Howard Hinnant39213642013-07-23 22:01:58 +00002156#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002157 }
2158 }
2159 }
2160 return end();
2161}
2162
2163template <class _Tp, class _Hash, class _Equal, class _Alloc>
2164template <class _Key>
2165typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
2166__hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k) const
2167{
2168 size_t __hash = hash_function()(__k);
2169 size_type __bc = bucket_count();
2170 if (__bc != 0)
2171 {
Howard Hinnant7a445152012-07-06 17:31:14 +00002172 size_t __chash = __constrain_hash(__hash, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002173 __node_const_pointer __nd = __bucket_list_[__chash];
2174 if (__nd != nullptr)
2175 {
2176 for (__nd = __nd->__next_; __nd != nullptr &&
Howard Hinnant7a445152012-07-06 17:31:14 +00002177 __constrain_hash(__nd->__hash_, __bc) == __chash;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002178 __nd = __nd->__next_)
2179 {
2180 if (key_eq()(__nd->__value_, __k))
Howard Hinnant39213642013-07-23 22:01:58 +00002181#if _LIBCPP_DEBUG_LEVEL >= 2
2182 return const_iterator(__nd, this);
2183#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002184 return const_iterator(__nd);
Howard Hinnant39213642013-07-23 22:01:58 +00002185#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002186 }
2187 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002188
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002189 }
2190 return end();
2191}
2192
Eric Fiselier2960ae22016-02-11 11:59:44 +00002193#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002194
2195template <class _Tp, class _Hash, class _Equal, class _Alloc>
2196template <class ..._Args>
2197typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
2198__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(_Args&& ...__args)
2199{
Eric Fiselier2960ae22016-02-11 11:59:44 +00002200 static_assert(!__is_hash_value_type<_Args...>::value,
2201 "Construct cannot be called with a hash value type");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002202 __node_allocator& __na = __node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00002203 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselier2960ae22016-02-11 11:59:44 +00002204 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002205 __h.get_deleter().__value_constructed = true;
2206 __h->__hash_ = hash_function()(__h->__value_);
2207 __h->__next_ = nullptr;
2208 return __h;
2209}
2210
2211template <class _Tp, class _Hash, class _Equal, class _Alloc>
Eric Fiselier2960ae22016-02-11 11:59:44 +00002212template <class _First, class ..._Rest>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002213typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Eric Fiselier2960ae22016-02-11 11:59:44 +00002214__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node_hash(
2215 size_t __hash, _First&& __f, _Rest&& ...__rest)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002216{
Eric Fiselier2960ae22016-02-11 11:59:44 +00002217 static_assert(!__is_hash_value_type<_First, _Rest...>::value,
2218 "Construct cannot be called with a hash value type");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002219 __node_allocator& __na = __node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00002220 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselier2960ae22016-02-11 11:59:44 +00002221 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_),
2222 _VSTD::forward<_First>(__f),
2223 _VSTD::forward<_Rest>(__rest)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002224 __h.get_deleter().__value_constructed = true;
2225 __h->__hash_ = __hash;
2226 __h->__next_ = nullptr;
Howard Hinnant9a894d92013-08-22 18:29:50 +00002227 return __h;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002228}
2229
Eric Fiselier2960ae22016-02-11 11:59:44 +00002230#else // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002231
2232template <class _Tp, class _Hash, class _Equal, class _Alloc>
2233typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Eric Fiselier2960ae22016-02-11 11:59:44 +00002234__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(const __container_value_type& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002235{
2236 __node_allocator& __na = __node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00002237 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselier2960ae22016-02-11 11:59:44 +00002238 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002239 __h.get_deleter().__value_constructed = true;
2240 __h->__hash_ = hash_function()(__h->__value_);
2241 __h->__next_ = nullptr;
Dimitry Andric89663502015-08-19 06:43:33 +00002242 return _LIBCPP_EXPLICIT_MOVE(__h); // explicitly moved for C++03
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002243}
2244
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002245template <class _Tp, class _Hash, class _Equal, class _Alloc>
2246typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Eric Fiselier2960ae22016-02-11 11:59:44 +00002247__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node_hash(size_t __hash,
2248 const __container_value_type& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002249{
2250 __node_allocator& __na = __node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00002251 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselier2960ae22016-02-11 11:59:44 +00002252 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002253 __h.get_deleter().__value_constructed = true;
2254 __h->__hash_ = __hash;
2255 __h->__next_ = nullptr;
Dimitry Andric89663502015-08-19 06:43:33 +00002256 return _LIBCPP_EXPLICIT_MOVE(__h); // explicitly moved for C++03
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002257}
2258
Eric Fiselier2960ae22016-02-11 11:59:44 +00002259#endif // _LIBCPP_CXX03_LANG
2260
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002261template <class _Tp, class _Hash, class _Equal, class _Alloc>
2262typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2263__hash_table<_Tp, _Hash, _Equal, _Alloc>::erase(const_iterator __p)
2264{
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00002265 __node_pointer __np = __p.__node_;
Howard Hinnant39213642013-07-23 22:01:58 +00002266#if _LIBCPP_DEBUG_LEVEL >= 2
2267 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2268 "unordered container erase(iterator) called with an iterator not"
2269 " referring to this container");
2270 _LIBCPP_ASSERT(__p != end(),
2271 "unordered container erase(iterator) called with a non-dereferenceable iterator");
2272 iterator __r(__np, this);
2273#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002274 iterator __r(__np);
Howard Hinnant39213642013-07-23 22:01:58 +00002275#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002276 ++__r;
2277 remove(__p);
2278 return __r;
2279}
2280
2281template <class _Tp, class _Hash, class _Equal, class _Alloc>
2282typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2283__hash_table<_Tp, _Hash, _Equal, _Alloc>::erase(const_iterator __first,
2284 const_iterator __last)
2285{
Howard Hinnant39213642013-07-23 22:01:58 +00002286#if _LIBCPP_DEBUG_LEVEL >= 2
2287 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
2288 "unodered container::erase(iterator, iterator) called with an iterator not"
2289 " referring to this unodered container");
2290 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__last) == this,
2291 "unodered container::erase(iterator, iterator) called with an iterator not"
2292 " referring to this unodered container");
2293#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002294 for (const_iterator __p = __first; __first != __last; __p = __first)
2295 {
2296 ++__first;
2297 erase(__p);
2298 }
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00002299 __node_pointer __np = __last.__node_;
Howard Hinnant39213642013-07-23 22:01:58 +00002300#if _LIBCPP_DEBUG_LEVEL >= 2
2301 return iterator (__np, this);
2302#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002303 return iterator (__np);
Howard Hinnant39213642013-07-23 22:01:58 +00002304#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002305}
2306
2307template <class _Tp, class _Hash, class _Equal, class _Alloc>
2308template <class _Key>
2309typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2310__hash_table<_Tp, _Hash, _Equal, _Alloc>::__erase_unique(const _Key& __k)
2311{
2312 iterator __i = find(__k);
2313 if (__i == end())
2314 return 0;
2315 erase(__i);
2316 return 1;
2317}
2318
2319template <class _Tp, class _Hash, class _Equal, class _Alloc>
2320template <class _Key>
2321typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2322__hash_table<_Tp, _Hash, _Equal, _Alloc>::__erase_multi(const _Key& __k)
2323{
2324 size_type __r = 0;
2325 iterator __i = find(__k);
2326 if (__i != end())
2327 {
2328 iterator __e = end();
2329 do
2330 {
2331 erase(__i++);
2332 ++__r;
2333 } while (__i != __e && key_eq()(*__i, __k));
2334 }
2335 return __r;
2336}
2337
2338template <class _Tp, class _Hash, class _Equal, class _Alloc>
2339typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00002340__hash_table<_Tp, _Hash, _Equal, _Alloc>::remove(const_iterator __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002341{
2342 // current node
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00002343 __node_pointer __cn = __p.__node_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002344 size_type __bc = bucket_count();
Howard Hinnant7a445152012-07-06 17:31:14 +00002345 size_t __chash = __constrain_hash(__cn->__hash_, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002346 // find previous node
2347 __node_pointer __pn = __bucket_list_[__chash];
2348 for (; __pn->__next_ != __cn; __pn = __pn->__next_)
2349 ;
2350 // Fix up __bucket_list_
2351 // if __pn is not in same bucket (before begin is not in same bucket) &&
2352 // if __cn->__next_ is not in same bucket (nullptr is not in same bucket)
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00002353 if (__pn == static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()))
2354 || __constrain_hash(__pn->__hash_, __bc) != __chash)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002355 {
Howard Hinnant7a445152012-07-06 17:31:14 +00002356 if (__cn->__next_ == nullptr || __constrain_hash(__cn->__next_->__hash_, __bc) != __chash)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002357 __bucket_list_[__chash] = nullptr;
2358 }
2359 // if __cn->__next_ is not in same bucket (nullptr is in same bucket)
2360 if (__cn->__next_ != nullptr)
2361 {
Howard Hinnant7a445152012-07-06 17:31:14 +00002362 size_t __nhash = __constrain_hash(__cn->__next_->__hash_, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002363 if (__nhash != __chash)
2364 __bucket_list_[__nhash] = __pn;
2365 }
2366 // remove __cn
2367 __pn->__next_ = __cn->__next_;
2368 __cn->__next_ = nullptr;
2369 --size();
Howard Hinnant39213642013-07-23 22:01:58 +00002370#if _LIBCPP_DEBUG_LEVEL >= 2
2371 __c_node* __c = __get_db()->__find_c_and_lock(this);
2372 for (__i_node** __p = __c->end_; __p != __c->beg_; )
2373 {
2374 --__p;
2375 iterator* __i = static_cast<iterator*>((*__p)->__i_);
2376 if (__i->__node_ == __cn)
2377 {
2378 (*__p)->__c_ = nullptr;
2379 if (--__c->end_ != __p)
2380 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
2381 }
2382 }
2383 __get_db()->unlock();
2384#endif
Howard Hinnant99968442011-11-29 18:15:50 +00002385 return __node_holder(__cn, _Dp(__node_alloc(), true));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002386}
2387
2388template <class _Tp, class _Hash, class _Equal, class _Alloc>
2389template <class _Key>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00002390inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002391typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2392__hash_table<_Tp, _Hash, _Equal, _Alloc>::__count_unique(const _Key& __k) const
2393{
2394 return static_cast<size_type>(find(__k) != end());
2395}
2396
2397template <class _Tp, class _Hash, class _Equal, class _Alloc>
2398template <class _Key>
2399typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2400__hash_table<_Tp, _Hash, _Equal, _Alloc>::__count_multi(const _Key& __k) const
2401{
2402 size_type __r = 0;
2403 const_iterator __i = find(__k);
2404 if (__i != end())
2405 {
2406 const_iterator __e = end();
2407 do
2408 {
2409 ++__i;
2410 ++__r;
2411 } while (__i != __e && key_eq()(*__i, __k));
2412 }
2413 return __r;
2414}
2415
2416template <class _Tp, class _Hash, class _Equal, class _Alloc>
2417template <class _Key>
2418pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator,
2419 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator>
2420__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_unique(
2421 const _Key& __k)
2422{
2423 iterator __i = find(__k);
2424 iterator __j = __i;
2425 if (__i != end())
2426 ++__j;
2427 return pair<iterator, iterator>(__i, __j);
2428}
2429
2430template <class _Tp, class _Hash, class _Equal, class _Alloc>
2431template <class _Key>
2432pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator,
2433 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator>
2434__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_unique(
2435 const _Key& __k) const
2436{
2437 const_iterator __i = find(__k);
2438 const_iterator __j = __i;
2439 if (__i != end())
2440 ++__j;
2441 return pair<const_iterator, const_iterator>(__i, __j);
2442}
2443
2444template <class _Tp, class _Hash, class _Equal, class _Alloc>
2445template <class _Key>
2446pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator,
2447 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator>
2448__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_multi(
2449 const _Key& __k)
2450{
2451 iterator __i = find(__k);
2452 iterator __j = __i;
2453 if (__i != end())
2454 {
2455 iterator __e = end();
2456 do
2457 {
2458 ++__j;
2459 } while (__j != __e && key_eq()(*__j, __k));
2460 }
2461 return pair<iterator, iterator>(__i, __j);
2462}
2463
2464template <class _Tp, class _Hash, class _Equal, class _Alloc>
2465template <class _Key>
2466pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator,
2467 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator>
2468__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_multi(
2469 const _Key& __k) const
2470{
2471 const_iterator __i = find(__k);
2472 const_iterator __j = __i;
2473 if (__i != end())
2474 {
2475 const_iterator __e = end();
2476 do
2477 {
2478 ++__j;
2479 } while (__j != __e && key_eq()(*__j, __k));
2480 }
2481 return pair<const_iterator, const_iterator>(__i, __j);
2482}
2483
2484template <class _Tp, class _Hash, class _Equal, class _Alloc>
2485void
2486__hash_table<_Tp, _Hash, _Equal, _Alloc>::swap(__hash_table& __u)
Eric Fiselier692177d2015-07-18 20:40:46 +00002487#if _LIBCPP_STD_VER <= 11
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00002488 _NOEXCEPT_(
Marshall Clow7d914d12015-07-13 20:04:56 +00002489 __is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value
Marshall Clow7d914d12015-07-13 20:04:56 +00002490 && (!allocator_traits<__pointer_allocator>::propagate_on_container_swap::value
2491 || __is_nothrow_swappable<__pointer_allocator>::value)
2492 && (!__node_traits::propagate_on_container_swap::value
2493 || __is_nothrow_swappable<__node_allocator>::value)
Marshall Clow7d914d12015-07-13 20:04:56 +00002494 )
Eric Fiselier692177d2015-07-18 20:40:46 +00002495#else
2496 _NOEXCEPT_(__is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value)
2497#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002498{
2499 {
2500 __node_pointer_pointer __npp = __bucket_list_.release();
2501 __bucket_list_.reset(__u.__bucket_list_.release());
2502 __u.__bucket_list_.reset(__npp);
2503 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002504 _VSTD::swap(__bucket_list_.get_deleter().size(), __u.__bucket_list_.get_deleter().size());
Marshall Clow7d914d12015-07-13 20:04:56 +00002505 __swap_allocator(__bucket_list_.get_deleter().__alloc(),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002506 __u.__bucket_list_.get_deleter().__alloc());
Marshall Clow7d914d12015-07-13 20:04:56 +00002507 __swap_allocator(__node_alloc(), __u.__node_alloc());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002508 _VSTD::swap(__p1_.first().__next_, __u.__p1_.first().__next_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002509 __p2_.swap(__u.__p2_);
2510 __p3_.swap(__u.__p3_);
2511 if (size() > 0)
Howard Hinnant7a445152012-07-06 17:31:14 +00002512 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash_, bucket_count())] =
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00002513 static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002514 if (__u.size() > 0)
Howard Hinnant7a445152012-07-06 17:31:14 +00002515 __u.__bucket_list_[__constrain_hash(__u.__p1_.first().__next_->__hash_, __u.bucket_count())] =
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00002516 static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__u.__p1_.first()));
Howard Hinnant39213642013-07-23 22:01:58 +00002517#if _LIBCPP_DEBUG_LEVEL >= 2
2518 __get_db()->swap(this, &__u);
2519#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002520}
2521
2522template <class _Tp, class _Hash, class _Equal, class _Alloc>
2523typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2524__hash_table<_Tp, _Hash, _Equal, _Alloc>::bucket_size(size_type __n) const
2525{
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00002526 _LIBCPP_ASSERT(__n < bucket_count(),
2527 "unordered container::bucket_size(n) called with n >= bucket_count()");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002528 __node_const_pointer __np = __bucket_list_[__n];
2529 size_type __bc = bucket_count();
2530 size_type __r = 0;
2531 if (__np != nullptr)
2532 {
2533 for (__np = __np->__next_; __np != nullptr &&
Howard Hinnant7a445152012-07-06 17:31:14 +00002534 __constrain_hash(__np->__hash_, __bc) == __n;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002535 __np = __np->__next_, ++__r)
2536 ;
2537 }
2538 return __r;
2539}
2540
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00002541template <class _Tp, class _Hash, class _Equal, class _Alloc>
2542inline _LIBCPP_INLINE_VISIBILITY
2543void
2544swap(__hash_table<_Tp, _Hash, _Equal, _Alloc>& __x,
2545 __hash_table<_Tp, _Hash, _Equal, _Alloc>& __y)
2546 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
2547{
2548 __x.swap(__y);
2549}
2550
Howard Hinnant39213642013-07-23 22:01:58 +00002551#if _LIBCPP_DEBUG_LEVEL >= 2
2552
2553template <class _Tp, class _Hash, class _Equal, class _Alloc>
2554bool
2555__hash_table<_Tp, _Hash, _Equal, _Alloc>::__dereferenceable(const const_iterator* __i) const
2556{
2557 return __i->__node_ != nullptr;
2558}
2559
2560template <class _Tp, class _Hash, class _Equal, class _Alloc>
2561bool
2562__hash_table<_Tp, _Hash, _Equal, _Alloc>::__decrementable(const const_iterator*) const
2563{
2564 return false;
2565}
2566
2567template <class _Tp, class _Hash, class _Equal, class _Alloc>
2568bool
2569__hash_table<_Tp, _Hash, _Equal, _Alloc>::__addable(const const_iterator*, ptrdiff_t) const
2570{
2571 return false;
2572}
2573
2574template <class _Tp, class _Hash, class _Equal, class _Alloc>
2575bool
2576__hash_table<_Tp, _Hash, _Equal, _Alloc>::__subscriptable(const const_iterator*, ptrdiff_t) const
2577{
2578 return false;
2579}
2580
2581#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002582_LIBCPP_END_NAMESPACE_STD
2583
2584#endif // _LIBCPP__HASH_TABLE