blob: 8f6fc4d7f71a40e3bfa31a27a87fb08b7abaeb95 [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
Duncan P. N. Exon Smith798ec842016-03-17 20:45:20 +0000103
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000104template <class _Tp, class _Hash, class _Equal, class _Alloc> class __hash_table;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000105
106template <class _NodePtr> class _LIBCPP_TYPE_VIS_ONLY __hash_iterator;
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000107template <class _ConstNodePtr> class _LIBCPP_TYPE_VIS_ONLY __hash_const_iterator;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000108template <class _NodePtr> class _LIBCPP_TYPE_VIS_ONLY __hash_local_iterator;
109template <class _ConstNodePtr> class _LIBCPP_TYPE_VIS_ONLY __hash_const_local_iterator;
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000110template <class _HashIterator> class _LIBCPP_TYPE_VIS_ONLY __hash_map_iterator;
111template <class _HashIterator> class _LIBCPP_TYPE_VIS_ONLY __hash_map_const_iterator;
Dan Albert99ca8202015-03-04 14:08:24 -0800112template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
113 class _LIBCPP_TYPE_VIS_ONLY unordered_map;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000114
Eric Fiselier774c7c52016-02-10 20:46:23 +0000115template <class _Tp>
Eric Fiselier66e344f2016-02-20 07:59:16 +0000116struct __hash_key_value_types {
Eric Fiselier774c7c52016-02-10 20:46:23 +0000117 static_assert(!is_reference<_Tp>::value && !is_const<_Tp>::value, "");
118 typedef _Tp key_type;
119 typedef _Tp __node_value_type;
120 typedef _Tp __container_value_type;
121 static const bool __is_map = false;
Eric Fiselier2960ae22016-02-11 11:59:44 +0000122
123 _LIBCPP_INLINE_VISIBILITY
124 static key_type const& __get_key(_Tp const& __v) {
125 return __v;
126 }
127 _LIBCPP_INLINE_VISIBILITY
128 static __container_value_type const& __get_value(__node_value_type const& __v) {
129 return __v;
130 }
131 _LIBCPP_INLINE_VISIBILITY
132 static __container_value_type* __get_ptr(__node_value_type& __n) {
133 return _VSTD::addressof(__n);
134 }
135#ifndef _LIBCPP_CXX03_LANG
136 _LIBCPP_INLINE_VISIBILITY
137 static __container_value_type&& __move(__node_value_type& __v) {
138 return _VSTD::move(__v);
139 }
140#endif
Eric Fiselier774c7c52016-02-10 20:46:23 +0000141};
142
143template <class _Key, class _Tp>
Eric Fiselier66e344f2016-02-20 07:59:16 +0000144struct __hash_key_value_types<__hash_value_type<_Key, _Tp> > {
Eric Fiselier774c7c52016-02-10 20:46:23 +0000145 typedef _Key key_type;
146 typedef _Tp mapped_type;
147 typedef __hash_value_type<_Key, _Tp> __node_value_type;
148 typedef pair<const _Key, _Tp> __container_value_type;
Eric Fiselier2960ae22016-02-11 11:59:44 +0000149 typedef pair<_Key, _Tp> __nc_value_type;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000150 typedef __container_value_type __map_value_type;
151 static const bool __is_map = true;
Eric Fiselier2960ae22016-02-11 11:59:44 +0000152
153 _LIBCPP_INLINE_VISIBILITY
154 static key_type const& __get_key(__container_value_type const& __v) {
155 return __v.first;
156 }
157
158 template <class _Up>
159 _LIBCPP_INLINE_VISIBILITY
160 static typename enable_if<__is_same_uncvref<_Up, __node_value_type>::value,
161 __container_value_type const&>::type
162 __get_value(_Up& __t) {
163 return __t.__cc;
164 }
165
166 template <class _Up>
167 _LIBCPP_INLINE_VISIBILITY
168 static typename enable_if<__is_same_uncvref<_Up, __container_value_type>::value,
169 __container_value_type const&>::type
170 __get_value(_Up& __t) {
171 return __t;
172 }
173
174 _LIBCPP_INLINE_VISIBILITY
175 static __container_value_type* __get_ptr(__node_value_type& __n) {
176 return _VSTD::addressof(__n.__cc);
177 }
178#ifndef _LIBCPP_CXX03_LANG
179 _LIBCPP_INLINE_VISIBILITY
180 static __nc_value_type&& __move(__node_value_type& __v) {
181 return _VSTD::move(__v.__nc);
182 }
183#endif
184
Eric Fiselier774c7c52016-02-10 20:46:23 +0000185};
186
Eric Fiselier66e344f2016-02-20 07:59:16 +0000187template <class _Tp, class _AllocPtr, class _KVTypes = __hash_key_value_types<_Tp>,
Eric Fiselier774c7c52016-02-10 20:46:23 +0000188 bool = _KVTypes::__is_map>
Eric Fiselier66e344f2016-02-20 07:59:16 +0000189struct __hash_map_pointer_types {};
Eric Fiselier774c7c52016-02-10 20:46:23 +0000190
191template <class _Tp, class _AllocPtr, class _KVTypes>
Eric Fiselier66e344f2016-02-20 07:59:16 +0000192struct __hash_map_pointer_types<_Tp, _AllocPtr, _KVTypes, true> {
Eric Fiselier774c7c52016-02-10 20:46:23 +0000193 typedef typename _KVTypes::__map_value_type _Mv;
194 typedef typename __rebind_pointer<_AllocPtr, _Mv>::type
195 __map_value_type_pointer;
196 typedef typename __rebind_pointer<_AllocPtr, const _Mv>::type
197 __const_map_value_type_pointer;
198};
199
200template <class _NodePtr, class _NodeT = typename pointer_traits<_NodePtr>::element_type>
201struct __hash_node_types;
202
203template <class _NodePtr, class _Tp, class _VoidPtr>
204struct __hash_node_types<_NodePtr, __hash_node<_Tp, _VoidPtr> >
Eric Fiselier66e344f2016-02-20 07:59:16 +0000205 : public __hash_key_value_types<_Tp>, __hash_map_pointer_types<_Tp, _VoidPtr>
Eric Fiselier774c7c52016-02-10 20:46:23 +0000206
207{
Eric Fiselier66e344f2016-02-20 07:59:16 +0000208 typedef __hash_key_value_types<_Tp> __base;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000209
210public:
211 typedef ptrdiff_t difference_type;
212 typedef size_t size_type;
213
214 typedef typename __rebind_pointer<_NodePtr, void>::type __void_pointer;
215
216 typedef typename pointer_traits<_NodePtr>::element_type __node_type;
217 typedef _NodePtr __node_pointer;
218
219 typedef __hash_node_base<__node_pointer> __node_base_type;
220 typedef typename __rebind_pointer<_NodePtr, __node_base_type>::type
221 __node_base_pointer;
222
223 typedef _Tp __node_value_type;
224 typedef typename __rebind_pointer<_VoidPtr, __node_value_type>::type
225 __node_value_type_pointer;
226 typedef typename __rebind_pointer<_VoidPtr, const __node_value_type>::type
227 __const_node_value_type_pointer;
228private:
229 static_assert(!is_const<__node_type>::value,
230 "_NodePtr should never be a pointer to const");
231 static_assert((is_same<typename pointer_traits<_VoidPtr>::element_type, void>::value),
232 "_VoidPtr does not point to unqualified void type");
233 static_assert((is_same<typename __rebind_pointer<_VoidPtr, __node_type>::type,
234 _NodePtr>::value), "_VoidPtr does not rebind to _NodePtr.");
235};
236
237
238
239template <class _HashIterator>
240struct __hash_node_types_from_iterator;
241template <class _NodePtr>
242struct __hash_node_types_from_iterator<__hash_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
243template <class _NodePtr>
244struct __hash_node_types_from_iterator<__hash_const_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
245template <class _NodePtr>
246struct __hash_node_types_from_iterator<__hash_local_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
247template <class _NodePtr>
248struct __hash_node_types_from_iterator<__hash_const_local_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
249
250
251template <class _NodeValueTp, class _VoidPtr>
252struct __make_hash_node_types {
253 typedef __hash_node<_NodeValueTp, _VoidPtr> _NodeTp;
254 typedef typename __rebind_pointer<_VoidPtr, _NodeTp>::type _NodePtr;
255 typedef __hash_node_types<_NodePtr> type;
256};
257
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000258template <class _NodePtr>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000259class _LIBCPP_TYPE_VIS_ONLY __hash_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000260{
Eric Fiselier774c7c52016-02-10 20:46:23 +0000261 typedef __hash_node_types<_NodePtr> _NodeTypes;
262 typedef _NodePtr __node_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000263
264 __node_pointer __node_;
265
266public:
Eric Fiselier774c7c52016-02-10 20:46:23 +0000267 typedef forward_iterator_tag iterator_category;
268 typedef typename _NodeTypes::__node_value_type value_type;
269 typedef typename _NodeTypes::difference_type difference_type;
270 typedef value_type& reference;
271 typedef typename _NodeTypes::__node_value_type_pointer pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000272
Howard Hinnant39213642013-07-23 22:01:58 +0000273 _LIBCPP_INLINE_VISIBILITY __hash_iterator() _NOEXCEPT
Marshall Clow193ef032013-08-07 21:30:44 +0000274#if _LIBCPP_STD_VER > 11
275 : __node_(nullptr)
276#endif
Howard Hinnant39213642013-07-23 22:01:58 +0000277 {
278#if _LIBCPP_DEBUG_LEVEL >= 2
279 __get_db()->__insert_i(this);
280#endif
281 }
282
283#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000284
Howard Hinnant99acc502010-09-21 17:32:39 +0000285 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000286 __hash_iterator(const __hash_iterator& __i)
287 : __node_(__i.__node_)
288 {
289 __get_db()->__iterator_copy(this, &__i);
290 }
291
Howard Hinnant99acc502010-09-21 17:32:39 +0000292 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000293 ~__hash_iterator()
294 {
295 __get_db()->__erase_i(this);
296 }
297
298 _LIBCPP_INLINE_VISIBILITY
299 __hash_iterator& operator=(const __hash_iterator& __i)
300 {
301 if (this != &__i)
302 {
303 __get_db()->__iterator_copy(this, &__i);
304 __node_ = __i.__node_;
305 }
306 return *this;
307 }
308
309#endif // _LIBCPP_DEBUG_LEVEL >= 2
310
311 _LIBCPP_INLINE_VISIBILITY
312 reference operator*() const
313 {
314#if _LIBCPP_DEBUG_LEVEL >= 2
315 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
316 "Attempted to dereference a non-dereferenceable unordered container iterator");
317#endif
318 return __node_->__value_;
319 }
320 _LIBCPP_INLINE_VISIBILITY
321 pointer operator->() const
322 {
323#if _LIBCPP_DEBUG_LEVEL >= 2
324 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
325 "Attempted to dereference a non-dereferenceable unordered container iterator");
326#endif
327 return pointer_traits<pointer>::pointer_to(__node_->__value_);
328 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000329
Howard Hinnant99acc502010-09-21 17:32:39 +0000330 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000331 __hash_iterator& operator++()
332 {
Howard Hinnant39213642013-07-23 22:01:58 +0000333#if _LIBCPP_DEBUG_LEVEL >= 2
334 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
335 "Attempted to increment non-incrementable unordered container iterator");
336#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000337 __node_ = __node_->__next_;
338 return *this;
339 }
340
Howard Hinnant99acc502010-09-21 17:32:39 +0000341 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000342 __hash_iterator operator++(int)
343 {
344 __hash_iterator __t(*this);
345 ++(*this);
346 return __t;
347 }
348
Howard Hinnant99acc502010-09-21 17:32:39 +0000349 friend _LIBCPP_INLINE_VISIBILITY
350 bool operator==(const __hash_iterator& __x, const __hash_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58 +0000351 {
Howard Hinnant39213642013-07-23 22:01:58 +0000352 return __x.__node_ == __y.__node_;
353 }
Howard Hinnant99acc502010-09-21 17:32:39 +0000354 friend _LIBCPP_INLINE_VISIBILITY
355 bool operator!=(const __hash_iterator& __x, const __hash_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58 +0000356 {return !(__x == __y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000357
358private:
Howard Hinnant39213642013-07-23 22:01:58 +0000359#if _LIBCPP_DEBUG_LEVEL >= 2
360 _LIBCPP_INLINE_VISIBILITY
361 __hash_iterator(__node_pointer __node, const void* __c) _NOEXCEPT
362 : __node_(__node)
363 {
364 __get_db()->__insert_ic(this, __c);
365 }
366#else
Howard Hinnant99acc502010-09-21 17:32:39 +0000367 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000368 __hash_iterator(__node_pointer __node) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000369 : __node_(__node)
370 {}
Howard Hinnant39213642013-07-23 22:01:58 +0000371#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000372
373 template <class, class, class, class> friend class __hash_table;
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000374 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_const_iterator;
375 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_map_iterator;
376 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_map;
377 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_multimap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000378};
379
Eric Fiselier774c7c52016-02-10 20:46:23 +0000380template <class _NodePtr>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000381class _LIBCPP_TYPE_VIS_ONLY __hash_const_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000382{
Eric Fiselier774c7c52016-02-10 20:46:23 +0000383 static_assert(!is_const<typename pointer_traits<_NodePtr>::element_type>::value, "");
384 typedef __hash_node_types<_NodePtr> _NodeTypes;
385 typedef _NodePtr __node_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000386
Eric Fiselier774c7c52016-02-10 20:46:23 +0000387 __node_pointer __node_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000388
389public:
Eric Fiselier0493d022016-02-18 00:20:34 +0000390 typedef __hash_iterator<_NodePtr> __non_const_iterator;
391
Eric Fiselier774c7c52016-02-10 20:46:23 +0000392 typedef forward_iterator_tag iterator_category;
393 typedef typename _NodeTypes::__node_value_type value_type;
394 typedef typename _NodeTypes::difference_type difference_type;
395 typedef const value_type& reference;
396 typedef typename _NodeTypes::__const_node_value_type_pointer pointer;
397
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000398
Howard Hinnant39213642013-07-23 22:01:58 +0000399 _LIBCPP_INLINE_VISIBILITY __hash_const_iterator() _NOEXCEPT
Marshall Clow193ef032013-08-07 21:30:44 +0000400#if _LIBCPP_STD_VER > 11
401 : __node_(nullptr)
402#endif
Howard Hinnant39213642013-07-23 22:01:58 +0000403 {
404#if _LIBCPP_DEBUG_LEVEL >= 2
405 __get_db()->__insert_i(this);
406#endif
407 }
Howard Hinnant99acc502010-09-21 17:32:39 +0000408 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000409 __hash_const_iterator(const __non_const_iterator& __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000410 : __node_(__x.__node_)
Howard Hinnant39213642013-07-23 22:01:58 +0000411 {
412#if _LIBCPP_DEBUG_LEVEL >= 2
413 __get_db()->__iterator_copy(this, &__x);
414#endif
415 }
416
417#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000418
Howard Hinnant99acc502010-09-21 17:32:39 +0000419 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000420 __hash_const_iterator(const __hash_const_iterator& __i)
421 : __node_(__i.__node_)
422 {
423 __get_db()->__iterator_copy(this, &__i);
424 }
425
Howard Hinnant99acc502010-09-21 17:32:39 +0000426 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000427 ~__hash_const_iterator()
428 {
429 __get_db()->__erase_i(this);
430 }
431
432 _LIBCPP_INLINE_VISIBILITY
433 __hash_const_iterator& operator=(const __hash_const_iterator& __i)
434 {
435 if (this != &__i)
436 {
437 __get_db()->__iterator_copy(this, &__i);
438 __node_ = __i.__node_;
439 }
440 return *this;
441 }
442
443#endif // _LIBCPP_DEBUG_LEVEL >= 2
444
445 _LIBCPP_INLINE_VISIBILITY
446 reference operator*() const
447 {
448#if _LIBCPP_DEBUG_LEVEL >= 2
449 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
450 "Attempted to dereference a non-dereferenceable unordered container const_iterator");
451#endif
452 return __node_->__value_;
453 }
454 _LIBCPP_INLINE_VISIBILITY
455 pointer operator->() const
456 {
457#if _LIBCPP_DEBUG_LEVEL >= 2
458 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
459 "Attempted to dereference a non-dereferenceable unordered container const_iterator");
460#endif
461 return pointer_traits<pointer>::pointer_to(__node_->__value_);
462 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000463
Howard Hinnant99acc502010-09-21 17:32:39 +0000464 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000465 __hash_const_iterator& operator++()
466 {
Howard Hinnant39213642013-07-23 22:01:58 +0000467#if _LIBCPP_DEBUG_LEVEL >= 2
468 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
469 "Attempted to increment non-incrementable unordered container const_iterator");
470#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000471 __node_ = __node_->__next_;
472 return *this;
473 }
474
Howard Hinnant99acc502010-09-21 17:32:39 +0000475 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000476 __hash_const_iterator operator++(int)
477 {
478 __hash_const_iterator __t(*this);
479 ++(*this);
480 return __t;
481 }
482
Howard 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 {
Howard Hinnant39213642013-07-23 22:01:58 +0000486 return __x.__node_ == __y.__node_;
487 }
Howard Hinnant99acc502010-09-21 17:32:39 +0000488 friend _LIBCPP_INLINE_VISIBILITY
489 bool operator!=(const __hash_const_iterator& __x, const __hash_const_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58 +0000490 {return !(__x == __y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000491
492private:
Howard Hinnant39213642013-07-23 22:01:58 +0000493#if _LIBCPP_DEBUG_LEVEL >= 2
494 _LIBCPP_INLINE_VISIBILITY
495 __hash_const_iterator(__node_pointer __node, const void* __c) _NOEXCEPT
496 : __node_(__node)
497 {
498 __get_db()->__insert_ic(this, __c);
499 }
500#else
Howard Hinnant99acc502010-09-21 17:32:39 +0000501 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000502 __hash_const_iterator(__node_pointer __node) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000503 : __node_(__node)
504 {}
Howard Hinnant39213642013-07-23 22:01:58 +0000505#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000506
507 template <class, class, class, class> friend class __hash_table;
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000508 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_map_const_iterator;
509 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_map;
510 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_multimap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000511};
512
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000513template <class _NodePtr>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000514class _LIBCPP_TYPE_VIS_ONLY __hash_local_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000515{
Eric Fiselier774c7c52016-02-10 20:46:23 +0000516 typedef __hash_node_types<_NodePtr> _NodeTypes;
517 typedef _NodePtr __node_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000518
519 __node_pointer __node_;
520 size_t __bucket_;
521 size_t __bucket_count_;
522
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000523public:
524 typedef forward_iterator_tag iterator_category;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000525 typedef typename _NodeTypes::__node_value_type value_type;
526 typedef typename _NodeTypes::difference_type difference_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000527 typedef value_type& reference;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000528 typedef typename _NodeTypes::__node_value_type_pointer pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000529
Howard Hinnant39213642013-07-23 22:01:58 +0000530 _LIBCPP_INLINE_VISIBILITY __hash_local_iterator() _NOEXCEPT
531 {
532#if _LIBCPP_DEBUG_LEVEL >= 2
533 __get_db()->__insert_i(this);
534#endif
535 }
536
537#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000538
Howard Hinnant99acc502010-09-21 17:32:39 +0000539 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000540 __hash_local_iterator(const __hash_local_iterator& __i)
541 : __node_(__i.__node_),
542 __bucket_(__i.__bucket_),
543 __bucket_count_(__i.__bucket_count_)
544 {
545 __get_db()->__iterator_copy(this, &__i);
546 }
547
Howard Hinnant99acc502010-09-21 17:32:39 +0000548 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000549 ~__hash_local_iterator()
550 {
551 __get_db()->__erase_i(this);
552 }
553
554 _LIBCPP_INLINE_VISIBILITY
555 __hash_local_iterator& operator=(const __hash_local_iterator& __i)
556 {
557 if (this != &__i)
558 {
559 __get_db()->__iterator_copy(this, &__i);
560 __node_ = __i.__node_;
561 __bucket_ = __i.__bucket_;
562 __bucket_count_ = __i.__bucket_count_;
563 }
564 return *this;
565 }
566
567#endif // _LIBCPP_DEBUG_LEVEL >= 2
568
569 _LIBCPP_INLINE_VISIBILITY
570 reference operator*() const
571 {
572#if _LIBCPP_DEBUG_LEVEL >= 2
573 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
574 "Attempted to dereference a non-dereferenceable unordered container local_iterator");
575#endif
576 return __node_->__value_;
577 }
578 _LIBCPP_INLINE_VISIBILITY
579 pointer operator->() const
580 {
581#if _LIBCPP_DEBUG_LEVEL >= 2
582 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
583 "Attempted to dereference a non-dereferenceable unordered container local_iterator");
584#endif
585 return pointer_traits<pointer>::pointer_to(__node_->__value_);
586 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000587
Howard Hinnant99acc502010-09-21 17:32:39 +0000588 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000589 __hash_local_iterator& operator++()
590 {
Howard Hinnant39213642013-07-23 22:01:58 +0000591#if _LIBCPP_DEBUG_LEVEL >= 2
592 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
593 "Attempted to increment non-incrementable unordered container local_iterator");
594#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000595 __node_ = __node_->__next_;
Howard Hinnant7a445152012-07-06 17:31:14 +0000596 if (__node_ != nullptr && __constrain_hash(__node_->__hash_, __bucket_count_) != __bucket_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000597 __node_ = nullptr;
598 return *this;
599 }
600
Howard Hinnant99acc502010-09-21 17:32:39 +0000601 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000602 __hash_local_iterator operator++(int)
603 {
604 __hash_local_iterator __t(*this);
605 ++(*this);
606 return __t;
607 }
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 {
Howard Hinnant39213642013-07-23 22:01:58 +0000612 return __x.__node_ == __y.__node_;
613 }
Howard Hinnant99acc502010-09-21 17:32:39 +0000614 friend _LIBCPP_INLINE_VISIBILITY
615 bool operator!=(const __hash_local_iterator& __x, const __hash_local_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58 +0000616 {return !(__x == __y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000617
618private:
Howard Hinnant39213642013-07-23 22:01:58 +0000619#if _LIBCPP_DEBUG_LEVEL >= 2
620 _LIBCPP_INLINE_VISIBILITY
621 __hash_local_iterator(__node_pointer __node, size_t __bucket,
622 size_t __bucket_count, const void* __c) _NOEXCEPT
623 : __node_(__node),
624 __bucket_(__bucket),
625 __bucket_count_(__bucket_count)
626 {
627 __get_db()->__insert_ic(this, __c);
628 if (__node_ != nullptr)
629 __node_ = __node_->__next_;
630 }
631#else
Howard Hinnant99acc502010-09-21 17:32:39 +0000632 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000633 __hash_local_iterator(__node_pointer __node, size_t __bucket,
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000634 size_t __bucket_count) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000635 : __node_(__node),
636 __bucket_(__bucket),
637 __bucket_count_(__bucket_count)
638 {
639 if (__node_ != nullptr)
640 __node_ = __node_->__next_;
641 }
Howard Hinnant39213642013-07-23 22:01:58 +0000642#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000643 template <class, class, class, class> friend class __hash_table;
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000644 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_const_local_iterator;
645 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_map_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000646};
647
648template <class _ConstNodePtr>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000649class _LIBCPP_TYPE_VIS_ONLY __hash_const_local_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000650{
Eric Fiselier774c7c52016-02-10 20:46:23 +0000651 typedef __hash_node_types<_ConstNodePtr> _NodeTypes;
652 typedef _ConstNodePtr __node_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000653
654 __node_pointer __node_;
655 size_t __bucket_;
656 size_t __bucket_count_;
657
658 typedef pointer_traits<__node_pointer> __pointer_traits;
659 typedef typename __pointer_traits::element_type __node;
660 typedef typename remove_const<__node>::type __non_const_node;
Eric Fiselier5cf84e02015-12-30 21:52:00 +0000661 typedef typename __rebind_pointer<__node_pointer, __non_const_node>::type
662 __non_const_node_pointer;
Eric Fiselier0493d022016-02-18 00:20:34 +0000663public:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000664 typedef __hash_local_iterator<__non_const_node_pointer>
665 __non_const_iterator;
Eric Fiselier0493d022016-02-18 00:20:34 +0000666
Eric Fiselier774c7c52016-02-10 20:46:23 +0000667 typedef forward_iterator_tag iterator_category;
668 typedef typename _NodeTypes::__node_value_type value_type;
669 typedef typename _NodeTypes::difference_type difference_type;
670 typedef const value_type& reference;
671 typedef typename _NodeTypes::__const_node_value_type_pointer pointer;
Eric Fiselier5cf84e02015-12-30 21:52:00 +0000672
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000673
Howard Hinnant39213642013-07-23 22:01:58 +0000674 _LIBCPP_INLINE_VISIBILITY __hash_const_local_iterator() _NOEXCEPT
675 {
676#if _LIBCPP_DEBUG_LEVEL >= 2
677 __get_db()->__insert_i(this);
678#endif
679 }
680
Howard Hinnant99acc502010-09-21 17:32:39 +0000681 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000682 __hash_const_local_iterator(const __non_const_iterator& __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000683 : __node_(__x.__node_),
684 __bucket_(__x.__bucket_),
685 __bucket_count_(__x.__bucket_count_)
Howard Hinnant39213642013-07-23 22:01:58 +0000686 {
687#if _LIBCPP_DEBUG_LEVEL >= 2
688 __get_db()->__iterator_copy(this, &__x);
689#endif
690 }
691
692#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000693
Howard Hinnant99acc502010-09-21 17:32:39 +0000694 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000695 __hash_const_local_iterator(const __hash_const_local_iterator& __i)
696 : __node_(__i.__node_),
697 __bucket_(__i.__bucket_),
698 __bucket_count_(__i.__bucket_count_)
699 {
700 __get_db()->__iterator_copy(this, &__i);
701 }
702
Howard Hinnant99acc502010-09-21 17:32:39 +0000703 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000704 ~__hash_const_local_iterator()
705 {
706 __get_db()->__erase_i(this);
707 }
708
709 _LIBCPP_INLINE_VISIBILITY
710 __hash_const_local_iterator& operator=(const __hash_const_local_iterator& __i)
711 {
712 if (this != &__i)
713 {
714 __get_db()->__iterator_copy(this, &__i);
715 __node_ = __i.__node_;
716 __bucket_ = __i.__bucket_;
717 __bucket_count_ = __i.__bucket_count_;
718 }
719 return *this;
720 }
721
722#endif // _LIBCPP_DEBUG_LEVEL >= 2
723
724 _LIBCPP_INLINE_VISIBILITY
725 reference operator*() const
726 {
727#if _LIBCPP_DEBUG_LEVEL >= 2
728 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
729 "Attempted to dereference a non-dereferenceable unordered container const_local_iterator");
730#endif
731 return __node_->__value_;
732 }
733 _LIBCPP_INLINE_VISIBILITY
734 pointer operator->() const
735 {
736#if _LIBCPP_DEBUG_LEVEL >= 2
737 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
738 "Attempted to dereference a non-dereferenceable unordered container const_local_iterator");
739#endif
740 return pointer_traits<pointer>::pointer_to(__node_->__value_);
741 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000742
Howard Hinnant99acc502010-09-21 17:32:39 +0000743 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000744 __hash_const_local_iterator& operator++()
745 {
Howard Hinnant39213642013-07-23 22:01:58 +0000746#if _LIBCPP_DEBUG_LEVEL >= 2
747 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
748 "Attempted to increment non-incrementable unordered container const_local_iterator");
749#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000750 __node_ = __node_->__next_;
Howard Hinnant7a445152012-07-06 17:31:14 +0000751 if (__node_ != nullptr && __constrain_hash(__node_->__hash_, __bucket_count_) != __bucket_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000752 __node_ = nullptr;
753 return *this;
754 }
755
Howard Hinnant99acc502010-09-21 17:32:39 +0000756 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000757 __hash_const_local_iterator operator++(int)
758 {
759 __hash_const_local_iterator __t(*this);
760 ++(*this);
761 return __t;
762 }
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 {
Howard Hinnant39213642013-07-23 22:01:58 +0000767 return __x.__node_ == __y.__node_;
768 }
Howard Hinnant99acc502010-09-21 17:32:39 +0000769 friend _LIBCPP_INLINE_VISIBILITY
770 bool operator!=(const __hash_const_local_iterator& __x, const __hash_const_local_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58 +0000771 {return !(__x == __y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000772
773private:
Howard Hinnant39213642013-07-23 22:01:58 +0000774#if _LIBCPP_DEBUG_LEVEL >= 2
775 _LIBCPP_INLINE_VISIBILITY
776 __hash_const_local_iterator(__node_pointer __node, size_t __bucket,
777 size_t __bucket_count, const void* __c) _NOEXCEPT
778 : __node_(__node),
779 __bucket_(__bucket),
780 __bucket_count_(__bucket_count)
781 {
782 __get_db()->__insert_ic(this, __c);
783 if (__node_ != nullptr)
784 __node_ = __node_->__next_;
785 }
786#else
Howard Hinnant99acc502010-09-21 17:32:39 +0000787 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000788 __hash_const_local_iterator(__node_pointer __node, size_t __bucket,
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000789 size_t __bucket_count) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000790 : __node_(__node),
791 __bucket_(__bucket),
792 __bucket_count_(__bucket_count)
793 {
794 if (__node_ != nullptr)
795 __node_ = __node_->__next_;
796 }
Howard Hinnant39213642013-07-23 22:01:58 +0000797#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000798 template <class, class, class, class> friend class __hash_table;
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000799 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_map_const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000800};
801
802template <class _Alloc>
803class __bucket_list_deallocator
804{
805 typedef _Alloc allocator_type;
806 typedef allocator_traits<allocator_type> __alloc_traits;
807 typedef typename __alloc_traits::size_type size_type;
808
809 __compressed_pair<size_type, allocator_type> __data_;
810public:
811 typedef typename __alloc_traits::pointer pointer;
812
Howard Hinnant99acc502010-09-21 17:32:39 +0000813 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000814 __bucket_list_deallocator()
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000815 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000816 : __data_(0) {}
Howard Hinnant99acc502010-09-21 17:32:39 +0000817
818 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000819 __bucket_list_deallocator(const allocator_type& __a, size_type __size)
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000820 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000821 : __data_(__size, __a) {}
822
Howard Hinnant73d21a42010-09-04 23:28:19 +0000823#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000824
Howard Hinnant99acc502010-09-21 17:32:39 +0000825 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000826 __bucket_list_deallocator(__bucket_list_deallocator&& __x)
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000827 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000828 : __data_(_VSTD::move(__x.__data_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000829 {
830 __x.size() = 0;
831 }
832
Howard Hinnant73d21a42010-09-04 23:28:19 +0000833#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000834
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000835 _LIBCPP_INLINE_VISIBILITY
836 size_type& size() _NOEXCEPT {return __data_.first();}
837 _LIBCPP_INLINE_VISIBILITY
838 size_type size() const _NOEXCEPT {return __data_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000839
Howard Hinnant99acc502010-09-21 17:32:39 +0000840 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000841 allocator_type& __alloc() _NOEXCEPT {return __data_.second();}
842 _LIBCPP_INLINE_VISIBILITY
843 const allocator_type& __alloc() const _NOEXCEPT {return __data_.second();}
844
845 _LIBCPP_INLINE_VISIBILITY
846 void operator()(pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000847 {
848 __alloc_traits::deallocate(__alloc(), __p, size());
849 }
850};
851
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000852template <class _Alloc> class __hash_map_node_destructor;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000853
854template <class _Alloc>
855class __hash_node_destructor
856{
857 typedef _Alloc allocator_type;
858 typedef allocator_traits<allocator_type> __alloc_traits;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000859
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000860public:
861 typedef typename __alloc_traits::pointer pointer;
862private:
Eric Fiselier2960ae22016-02-11 11:59:44 +0000863 typedef __hash_node_types<pointer> _NodeTypes;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000864
865 allocator_type& __na_;
866
867 __hash_node_destructor& operator=(const __hash_node_destructor&);
868
869public:
870 bool __value_constructed;
871
Howard Hinnant99acc502010-09-21 17:32:39 +0000872 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant199d0ae2011-07-31 17:04:30 +0000873 explicit __hash_node_destructor(allocator_type& __na,
874 bool __constructed = false) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000875 : __na_(__na),
Howard Hinnant199d0ae2011-07-31 17:04:30 +0000876 __value_constructed(__constructed)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000877 {}
878
Howard Hinnant99acc502010-09-21 17:32:39 +0000879 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000880 void operator()(pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000881 {
882 if (__value_constructed)
Eric Fiselier2960ae22016-02-11 11:59:44 +0000883 __alloc_traits::destroy(__na_, _NodeTypes::__get_ptr(__p->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000884 if (__p)
885 __alloc_traits::deallocate(__na_, __p, 1);
886 }
887
888 template <class> friend class __hash_map_node_destructor;
889};
890
891template <class _Tp, class _Hash, class _Equal, class _Alloc>
892class __hash_table
893{
894public:
895 typedef _Tp value_type;
896 typedef _Hash hasher;
897 typedef _Equal key_equal;
898 typedef _Alloc allocator_type;
899
900private:
901 typedef allocator_traits<allocator_type> __alloc_traits;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000902 typedef typename
903 __make_hash_node_types<value_type, typename __alloc_traits::void_pointer>::type
904 _NodeTypes;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000905public:
Eric Fiselier2960ae22016-02-11 11:59:44 +0000906
907 typedef typename _NodeTypes::__node_value_type __node_value_type;
908 typedef typename _NodeTypes::__container_value_type __container_value_type;
Duncan P. N. Exon Smith798ec842016-03-17 20:45:20 +0000909 typedef typename _NodeTypes::key_type key_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000910 typedef value_type& reference;
911 typedef const value_type& const_reference;
912 typedef typename __alloc_traits::pointer pointer;
913 typedef typename __alloc_traits::const_pointer const_pointer;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000914#ifndef _LIBCPP_ABI_FIX_UNORDERED_CONTAINER_SIZE_TYPE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000915 typedef typename __alloc_traits::size_type size_type;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000916#else
917 typedef typename _NodeTypes::size_type size_type;
918#endif
919 typedef typename _NodeTypes::difference_type difference_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000920public:
921 // Create __node
Eric Fiselier774c7c52016-02-10 20:46:23 +0000922
923 typedef typename _NodeTypes::__node_type __node;
Marshall Clow66302c62015-04-07 05:21:38 +0000924 typedef typename __rebind_alloc_helper<__alloc_traits, __node>::type __node_allocator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000925 typedef allocator_traits<__node_allocator> __node_traits;
Eric Fiselier9e9f42e2016-02-11 15:22:37 +0000926 typedef typename _NodeTypes::__void_pointer __void_pointer;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000927 typedef typename _NodeTypes::__node_pointer __node_pointer;
928 typedef typename _NodeTypes::__node_pointer __node_const_pointer;
929 typedef typename _NodeTypes::__node_base_type __first_node;
930 typedef typename _NodeTypes::__node_base_pointer __node_base_pointer;
931
932private:
933 // check for sane allocator pointer rebinding semantics. Rebinding the
934 // allocator for a new pointer type should be exactly the same as rebinding
935 // the pointer using 'pointer_traits'.
936 static_assert((is_same<__node_pointer, typename __node_traits::pointer>::value),
937 "Allocator does not rebind pointers in a sane manner.");
938 typedef typename __rebind_alloc_helper<__node_traits, __first_node>::type
939 __node_base_allocator;
940 typedef allocator_traits<__node_base_allocator> __node_base_traits;
941 static_assert((is_same<__node_base_pointer, typename __node_base_traits::pointer>::value),
942 "Allocator does not rebind pointers in a sane manner.");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000943
944private:
945
Marshall Clow66302c62015-04-07 05:21:38 +0000946 typedef typename __rebind_alloc_helper<__node_traits, __node_pointer>::type __pointer_allocator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000947 typedef __bucket_list_deallocator<__pointer_allocator> __bucket_list_deleter;
948 typedef unique_ptr<__node_pointer[], __bucket_list_deleter> __bucket_list;
949 typedef allocator_traits<__pointer_allocator> __pointer_alloc_traits;
950 typedef typename __bucket_list_deleter::pointer __node_pointer_pointer;
951
952 // --- Member data begin ---
Eric Fiselier774c7c52016-02-10 20:46:23 +0000953 __bucket_list __bucket_list_;
954 __compressed_pair<__first_node, __node_allocator> __p1_;
955 __compressed_pair<size_type, hasher> __p2_;
956 __compressed_pair<float, key_equal> __p3_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000957 // --- Member data end ---
958
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000959 _LIBCPP_INLINE_VISIBILITY
960 size_type& size() _NOEXCEPT {return __p2_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000961public:
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000962 _LIBCPP_INLINE_VISIBILITY
963 size_type size() const _NOEXCEPT {return __p2_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000964
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000965 _LIBCPP_INLINE_VISIBILITY
966 hasher& hash_function() _NOEXCEPT {return __p2_.second();}
967 _LIBCPP_INLINE_VISIBILITY
968 const hasher& hash_function() const _NOEXCEPT {return __p2_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000969
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000970 _LIBCPP_INLINE_VISIBILITY
971 float& max_load_factor() _NOEXCEPT {return __p3_.first();}
972 _LIBCPP_INLINE_VISIBILITY
973 float max_load_factor() const _NOEXCEPT {return __p3_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000974
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000975 _LIBCPP_INLINE_VISIBILITY
976 key_equal& key_eq() _NOEXCEPT {return __p3_.second();}
977 _LIBCPP_INLINE_VISIBILITY
978 const key_equal& key_eq() const _NOEXCEPT {return __p3_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000979
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000980 _LIBCPP_INLINE_VISIBILITY
981 __node_allocator& __node_alloc() _NOEXCEPT {return __p1_.second();}
982 _LIBCPP_INLINE_VISIBILITY
983 const __node_allocator& __node_alloc() const _NOEXCEPT
984 {return __p1_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000985
986public:
987 typedef __hash_iterator<__node_pointer> iterator;
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +0000988 typedef __hash_const_iterator<__node_pointer> const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000989 typedef __hash_local_iterator<__node_pointer> local_iterator;
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +0000990 typedef __hash_const_local_iterator<__node_pointer> const_local_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000991
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000992 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000993 __hash_table()
994 _NOEXCEPT_(
995 is_nothrow_default_constructible<__bucket_list>::value &&
996 is_nothrow_default_constructible<__first_node>::value &&
997 is_nothrow_default_constructible<__node_allocator>::value &&
998 is_nothrow_default_constructible<hasher>::value &&
999 is_nothrow_default_constructible<key_equal>::value);
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001000 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001001 __hash_table(const hasher& __hf, const key_equal& __eql);
1002 __hash_table(const hasher& __hf, const key_equal& __eql,
1003 const allocator_type& __a);
1004 explicit __hash_table(const allocator_type& __a);
1005 __hash_table(const __hash_table& __u);
1006 __hash_table(const __hash_table& __u, const allocator_type& __a);
Eric Fiselier2960ae22016-02-11 11:59:44 +00001007#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001008 __hash_table(__hash_table&& __u)
1009 _NOEXCEPT_(
1010 is_nothrow_move_constructible<__bucket_list>::value &&
1011 is_nothrow_move_constructible<__first_node>::value &&
1012 is_nothrow_move_constructible<__node_allocator>::value &&
1013 is_nothrow_move_constructible<hasher>::value &&
1014 is_nothrow_move_constructible<key_equal>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001015 __hash_table(__hash_table&& __u, const allocator_type& __a);
Eric Fiselier2960ae22016-02-11 11:59:44 +00001016#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001017 ~__hash_table();
1018
1019 __hash_table& operator=(const __hash_table& __u);
Eric Fiselier2960ae22016-02-11 11:59:44 +00001020#ifndef _LIBCPP_CXX03_LANG
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001021 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001022 __hash_table& operator=(__hash_table&& __u)
1023 _NOEXCEPT_(
1024 __node_traits::propagate_on_container_move_assignment::value &&
1025 is_nothrow_move_assignable<__node_allocator>::value &&
1026 is_nothrow_move_assignable<hasher>::value &&
1027 is_nothrow_move_assignable<key_equal>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001028#endif
1029 template <class _InputIterator>
1030 void __assign_unique(_InputIterator __first, _InputIterator __last);
1031 template <class _InputIterator>
1032 void __assign_multi(_InputIterator __first, _InputIterator __last);
1033
Howard Hinnant99acc502010-09-21 17:32:39 +00001034 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001035 size_type max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001036 {
1037 return allocator_traits<__pointer_allocator>::max_size(
1038 __bucket_list_.get_deleter().__alloc());
1039 }
1040
1041 pair<iterator, bool> __node_insert_unique(__node_pointer __nd);
1042 iterator __node_insert_multi(__node_pointer __nd);
1043 iterator __node_insert_multi(const_iterator __p,
1044 __node_pointer __nd);
1045
Eric Fiselier2960ae22016-02-11 11:59:44 +00001046#ifndef _LIBCPP_CXX03_LANG
1047 template <class _Key, class ..._Args>
Duncan P. N. Exon Smith798ec842016-03-17 20:45:20 +00001048 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2960ae22016-02-11 11:59:44 +00001049 pair<iterator, bool> __emplace_unique_key_args(_Key const& __k, _Args&&... __args);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001050
Eric Fiselier2960ae22016-02-11 11:59:44 +00001051 template <class... _Args>
Eric Fiselierfdae69a2015-06-13 07:18:32 +00001052 _LIBCPP_INLINE_VISIBILITY
Duncan P. N. Exon Smith798ec842016-03-17 20:45:20 +00001053 pair<iterator, bool> __emplace_unique_impl(_Args&&... __args);
1054
1055 template <class _Pp>
Eric Fiselierfdae69a2015-06-13 07:18:32 +00001056 _LIBCPP_INLINE_VISIBILITY
Duncan P. N. Exon Smith798ec842016-03-17 20:45:20 +00001057 pair<iterator, bool> __emplace_unique(_Pp&& __x) {
1058 return __emplace_unique_extract_key(_VSTD::forward<_Pp>(__x),
1059 __can_extract_key<_Pp, key_type>());
1060 }
Eric Fiselierdc414cd2016-04-16 00:23:12 +00001061
1062 template <class _First, class _Second>
1063 _LIBCPP_INLINE_VISIBILITY
1064 typename enable_if<
1065 __can_extract_map_key<_First, key_type, __container_value_type>::value,
1066 pair<iterator, bool>
1067 >::type __emplace_unique(_First&& __f, _Second&& __s) {
1068 return __emplace_unique_key_args(__f, _VSTD::forward<_First>(__f),
1069 _VSTD::forward<_Second>(__s));
1070 }
1071
Eric Fiselier2960ae22016-02-11 11:59:44 +00001072 template <class... _Args>
Duncan P. N. Exon Smith798ec842016-03-17 20:45:20 +00001073 _LIBCPP_INLINE_VISIBILITY
1074 pair<iterator, bool> __emplace_unique(_Args&&... __args) {
1075 return __emplace_unique_impl(_VSTD::forward<_Args>(__args)...);
1076 }
1077
1078 template <class _Pp>
1079 _LIBCPP_INLINE_VISIBILITY
1080 pair<iterator, bool>
1081 __emplace_unique_extract_key(_Pp&& __x, __extract_key_fail_tag) {
1082 return __emplace_unique_impl(_VSTD::forward<_Pp>(__x));
1083 }
1084 template <class _Pp>
1085 _LIBCPP_INLINE_VISIBILITY
1086 pair<iterator, bool>
1087 __emplace_unique_extract_key(_Pp&& __x, __extract_key_self_tag) {
1088 return __emplace_unique_key_args(__x, _VSTD::forward<_Pp>(__x));
1089 }
1090 template <class _Pp>
1091 _LIBCPP_INLINE_VISIBILITY
1092 pair<iterator, bool>
1093 __emplace_unique_extract_key(_Pp&& __x, __extract_key_first_tag) {
1094 return __emplace_unique_key_args(__x.first, _VSTD::forward<_Pp>(__x));
1095 }
1096
1097 template <class... _Args>
1098 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2960ae22016-02-11 11:59:44 +00001099 iterator __emplace_multi(_Args&&... __args);
1100 template <class... _Args>
Duncan P. N. Exon Smith798ec842016-03-17 20:45:20 +00001101 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2960ae22016-02-11 11:59:44 +00001102 iterator __emplace_hint_multi(const_iterator __p, _Args&&... __args);
1103
1104
Eric Fiselierfdae69a2015-06-13 07:18:32 +00001105 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2960ae22016-02-11 11:59:44 +00001106 pair<iterator, bool>
1107 __insert_unique(__container_value_type&& __x) {
1108 return __emplace_unique_key_args(_NodeTypes::__get_key(__x), _VSTD::move(__x));
1109 }
1110
1111 template <class _Pp, class = typename enable_if<
1112 !__is_same_uncvref<_Pp, __container_value_type>::value
1113 >::type>
Eric Fiselierfdae69a2015-06-13 07:18:32 +00001114 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2960ae22016-02-11 11:59:44 +00001115 pair<iterator, bool> __insert_unique(_Pp&& __x) {
1116 return __emplace_unique(_VSTD::forward<_Pp>(__x));
1117 }
1118
1119 template <class _Pp>
1120 _LIBCPP_INLINE_VISIBILITY
1121 iterator __insert_multi(_Pp&& __x) {
1122 return __emplace_multi(_VSTD::forward<_Pp>(__x));
1123 }
1124
1125 template <class _Pp>
1126 _LIBCPP_INLINE_VISIBILITY
1127 iterator __insert_multi(const_iterator __p, _Pp&& __x) {
1128 return __emplace_hint_multi(__p, _VSTD::forward<_Pp>(__x));
1129 }
1130
1131#else // !defined(_LIBCPP_CXX03_LANG)
1132 template <class _Key, class _Args>
1133 pair<iterator, bool> __emplace_unique_key_args(_Key const&, _Args& __args);
1134
1135 iterator __insert_multi(const __container_value_type& __x);
1136 iterator __insert_multi(const_iterator __p, const __container_value_type& __x);
Eric Fiselierfdae69a2015-06-13 07:18:32 +00001137#endif
1138
Eric Fiselier2960ae22016-02-11 11:59:44 +00001139 _LIBCPP_INLINE_VISIBILITY
1140 pair<iterator, bool> __insert_unique(const __container_value_type& __x) {
1141 return __emplace_unique_key_args(_NodeTypes::__get_key(__x), __x);
1142 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001143
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001144 void clear() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001145 void rehash(size_type __n);
Howard Hinnant99acc502010-09-21 17:32:39 +00001146 _LIBCPP_INLINE_VISIBILITY void reserve(size_type __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001147 {rehash(static_cast<size_type>(ceil(__n / max_load_factor())));}
Howard Hinnant99acc502010-09-21 17:32:39 +00001148
1149 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001150 size_type bucket_count() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001151 {
1152 return __bucket_list_.get_deleter().size();
1153 }
1154
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001155 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001156 iterator begin() _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001157 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001158 iterator end() _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001159 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001160 const_iterator begin() const _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001161 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001162 const_iterator end() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001163
1164 template <class _Key>
Howard Hinnant99acc502010-09-21 17:32:39 +00001165 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001166 size_type bucket(const _Key& __k) const
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00001167 {
1168 _LIBCPP_ASSERT(bucket_count() > 0,
1169 "unordered container::bucket(key) called when bucket_count() == 0");
1170 return __constrain_hash(hash_function()(__k), bucket_count());
1171 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001172
1173 template <class _Key>
1174 iterator find(const _Key& __x);
1175 template <class _Key>
1176 const_iterator find(const _Key& __x) const;
1177
Howard Hinnant99968442011-11-29 18:15:50 +00001178 typedef __hash_node_destructor<__node_allocator> _Dp;
1179 typedef unique_ptr<__node, _Dp> __node_holder;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001180
1181 iterator erase(const_iterator __p);
1182 iterator erase(const_iterator __first, const_iterator __last);
1183 template <class _Key>
1184 size_type __erase_unique(const _Key& __k);
1185 template <class _Key>
1186 size_type __erase_multi(const _Key& __k);
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001187 __node_holder remove(const_iterator __p) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001188
1189 template <class _Key>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001190 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001191 size_type __count_unique(const _Key& __k) const;
1192 template <class _Key>
1193 size_type __count_multi(const _Key& __k) const;
1194
1195 template <class _Key>
1196 pair<iterator, iterator>
1197 __equal_range_unique(const _Key& __k);
1198 template <class _Key>
1199 pair<const_iterator, const_iterator>
1200 __equal_range_unique(const _Key& __k) const;
1201
1202 template <class _Key>
1203 pair<iterator, iterator>
1204 __equal_range_multi(const _Key& __k);
1205 template <class _Key>
1206 pair<const_iterator, const_iterator>
1207 __equal_range_multi(const _Key& __k) const;
1208
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001209 void swap(__hash_table& __u)
Eric Fiselier692177d2015-07-18 20:40:46 +00001210#if _LIBCPP_STD_VER <= 11
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001211 _NOEXCEPT_(
Marshall Clow7d914d12015-07-13 20:04:56 +00001212 __is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value
Marshall Clow7d914d12015-07-13 20:04:56 +00001213 && (!allocator_traits<__pointer_allocator>::propagate_on_container_swap::value
1214 || __is_nothrow_swappable<__pointer_allocator>::value)
1215 && (!__node_traits::propagate_on_container_swap::value
1216 || __is_nothrow_swappable<__node_allocator>::value)
Marshall Clow7d914d12015-07-13 20:04:56 +00001217 );
Eric Fiselier692177d2015-07-18 20:40:46 +00001218#else
1219 _NOEXCEPT_(__is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value);
1220#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001221
Howard Hinnant99acc502010-09-21 17:32:39 +00001222 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001223 size_type max_bucket_count() const _NOEXCEPT
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001224 {return __pointer_alloc_traits::max_size(__bucket_list_.get_deleter().__alloc());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001225 size_type bucket_size(size_type __n) const;
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001226 _LIBCPP_INLINE_VISIBILITY float load_factor() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001227 {
1228 size_type __bc = bucket_count();
1229 return __bc != 0 ? (float)size() / __bc : 0.f;
1230 }
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001231 _LIBCPP_INLINE_VISIBILITY void max_load_factor(float __mlf) _NOEXCEPT
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00001232 {
1233 _LIBCPP_ASSERT(__mlf > 0,
1234 "unordered container::max_load_factor(lf) called with lf <= 0");
1235 max_load_factor() = _VSTD::max(__mlf, load_factor());
1236 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001237
Howard Hinnant39213642013-07-23 22:01:58 +00001238 _LIBCPP_INLINE_VISIBILITY
1239 local_iterator
1240 begin(size_type __n)
1241 {
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00001242 _LIBCPP_ASSERT(__n < bucket_count(),
1243 "unordered container::begin(n) called with n >= bucket_count()");
Howard Hinnant39213642013-07-23 22:01:58 +00001244#if _LIBCPP_DEBUG_LEVEL >= 2
1245 return local_iterator(__bucket_list_[__n], __n, bucket_count(), this);
1246#else
1247 return local_iterator(__bucket_list_[__n], __n, bucket_count());
1248#endif
1249 }
1250
1251 _LIBCPP_INLINE_VISIBILITY
1252 local_iterator
1253 end(size_type __n)
1254 {
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00001255 _LIBCPP_ASSERT(__n < bucket_count(),
1256 "unordered container::end(n) called with n >= bucket_count()");
Howard Hinnant39213642013-07-23 22:01:58 +00001257#if _LIBCPP_DEBUG_LEVEL >= 2
1258 return local_iterator(nullptr, __n, bucket_count(), this);
1259#else
1260 return local_iterator(nullptr, __n, bucket_count());
1261#endif
1262 }
1263
1264 _LIBCPP_INLINE_VISIBILITY
1265 const_local_iterator
1266 cbegin(size_type __n) const
1267 {
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00001268 _LIBCPP_ASSERT(__n < bucket_count(),
1269 "unordered container::cbegin(n) called with n >= bucket_count()");
Howard Hinnant39213642013-07-23 22:01:58 +00001270#if _LIBCPP_DEBUG_LEVEL >= 2
1271 return const_local_iterator(__bucket_list_[__n], __n, bucket_count(), this);
1272#else
1273 return const_local_iterator(__bucket_list_[__n], __n, bucket_count());
1274#endif
1275 }
1276
1277 _LIBCPP_INLINE_VISIBILITY
1278 const_local_iterator
1279 cend(size_type __n) const
1280 {
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00001281 _LIBCPP_ASSERT(__n < bucket_count(),
1282 "unordered container::cend(n) called with n >= bucket_count()");
Howard Hinnant39213642013-07-23 22:01:58 +00001283#if _LIBCPP_DEBUG_LEVEL >= 2
1284 return const_local_iterator(nullptr, __n, bucket_count(), this);
1285#else
1286 return const_local_iterator(nullptr, __n, bucket_count());
1287#endif
1288 }
1289
1290#if _LIBCPP_DEBUG_LEVEL >= 2
1291
1292 bool __dereferenceable(const const_iterator* __i) const;
1293 bool __decrementable(const const_iterator* __i) const;
1294 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1295 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1296
1297#endif // _LIBCPP_DEBUG_LEVEL >= 2
1298
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001299private:
1300 void __rehash(size_type __n);
1301
Eric Fiselier2960ae22016-02-11 11:59:44 +00001302#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001303 template <class ..._Args>
Eric Fiselier2960ae22016-02-11 11:59:44 +00001304 __node_holder __construct_node(_Args&& ...__args);
1305
1306 template <class _First, class ..._Rest>
1307 __node_holder __construct_node_hash(size_t __hash, _First&& __f, _Rest&&... __rest);
1308#else // _LIBCPP_CXX03_LANG
1309 __node_holder __construct_node(const __container_value_type& __v);
1310 __node_holder __construct_node_hash(size_t __hash, const __container_value_type& __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001311#endif
Eric Fiselier2960ae22016-02-11 11:59:44 +00001312
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001313
Howard Hinnant99acc502010-09-21 17:32:39 +00001314 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001315 void __copy_assign_alloc(const __hash_table& __u)
1316 {__copy_assign_alloc(__u, integral_constant<bool,
1317 __node_traits::propagate_on_container_copy_assignment::value>());}
1318 void __copy_assign_alloc(const __hash_table& __u, true_type);
Howard Hinnant99acc502010-09-21 17:32:39 +00001319 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00001320 void __copy_assign_alloc(const __hash_table&, false_type) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001321
Eric Fiselier2960ae22016-02-11 11:59:44 +00001322#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001323 void __move_assign(__hash_table& __u, false_type);
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001324 void __move_assign(__hash_table& __u, true_type)
1325 _NOEXCEPT_(
1326 is_nothrow_move_assignable<__node_allocator>::value &&
1327 is_nothrow_move_assignable<hasher>::value &&
1328 is_nothrow_move_assignable<key_equal>::value);
1329 _LIBCPP_INLINE_VISIBILITY
1330 void __move_assign_alloc(__hash_table& __u)
1331 _NOEXCEPT_(
1332 !__node_traits::propagate_on_container_move_assignment::value ||
1333 (is_nothrow_move_assignable<__pointer_allocator>::value &&
1334 is_nothrow_move_assignable<__node_allocator>::value))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001335 {__move_assign_alloc(__u, integral_constant<bool,
1336 __node_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnant99acc502010-09-21 17:32:39 +00001337 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001338 void __move_assign_alloc(__hash_table& __u, true_type)
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001339 _NOEXCEPT_(
1340 is_nothrow_move_assignable<__pointer_allocator>::value &&
1341 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001342 {
1343 __bucket_list_.get_deleter().__alloc() =
Howard Hinnant0949eed2011-06-30 21:18:19 +00001344 _VSTD::move(__u.__bucket_list_.get_deleter().__alloc());
1345 __node_alloc() = _VSTD::move(__u.__node_alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001346 }
Howard Hinnant99acc502010-09-21 17:32:39 +00001347 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001348 void __move_assign_alloc(__hash_table&, false_type) _NOEXCEPT {}
Eric Fiselier2960ae22016-02-11 11:59:44 +00001349#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001350
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001351 void __deallocate(__node_pointer __np) _NOEXCEPT;
1352 __node_pointer __detach() _NOEXCEPT;
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001353
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001354 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_map;
1355 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_multimap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001356};
1357
1358template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001359inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001360__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table()
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001361 _NOEXCEPT_(
1362 is_nothrow_default_constructible<__bucket_list>::value &&
1363 is_nothrow_default_constructible<__first_node>::value &&
Eric Fiselierc8f54c22015-12-16 00:53:04 +00001364 is_nothrow_default_constructible<__node_allocator>::value &&
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001365 is_nothrow_default_constructible<hasher>::value &&
1366 is_nothrow_default_constructible<key_equal>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001367 : __p2_(0),
1368 __p3_(1.0f)
1369{
1370}
1371
1372template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001373inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001374__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const hasher& __hf,
1375 const key_equal& __eql)
1376 : __bucket_list_(nullptr, __bucket_list_deleter()),
1377 __p1_(),
1378 __p2_(0, __hf),
1379 __p3_(1.0f, __eql)
1380{
1381}
1382
1383template <class _Tp, class _Hash, class _Equal, class _Alloc>
1384__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const hasher& __hf,
1385 const key_equal& __eql,
1386 const allocator_type& __a)
1387 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1388 __p1_(__node_allocator(__a)),
1389 __p2_(0, __hf),
1390 __p3_(1.0f, __eql)
1391{
1392}
1393
1394template <class _Tp, class _Hash, class _Equal, class _Alloc>
1395__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const allocator_type& __a)
1396 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1397 __p1_(__node_allocator(__a)),
1398 __p2_(0),
1399 __p3_(1.0f)
1400{
1401}
1402
1403template <class _Tp, class _Hash, class _Equal, class _Alloc>
1404__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const __hash_table& __u)
1405 : __bucket_list_(nullptr,
1406 __bucket_list_deleter(allocator_traits<__pointer_allocator>::
1407 select_on_container_copy_construction(
1408 __u.__bucket_list_.get_deleter().__alloc()), 0)),
1409 __p1_(allocator_traits<__node_allocator>::
1410 select_on_container_copy_construction(__u.__node_alloc())),
1411 __p2_(0, __u.hash_function()),
1412 __p3_(__u.__p3_)
1413{
1414}
1415
1416template <class _Tp, class _Hash, class _Equal, class _Alloc>
1417__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const __hash_table& __u,
1418 const allocator_type& __a)
1419 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1420 __p1_(__node_allocator(__a)),
1421 __p2_(0, __u.hash_function()),
1422 __p3_(__u.__p3_)
1423{
1424}
1425
Eric Fiselier2960ae22016-02-11 11:59:44 +00001426#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001427
1428template <class _Tp, class _Hash, class _Equal, class _Alloc>
1429__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(__hash_table&& __u)
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001430 _NOEXCEPT_(
1431 is_nothrow_move_constructible<__bucket_list>::value &&
1432 is_nothrow_move_constructible<__first_node>::value &&
Eric Fiselierc8f54c22015-12-16 00:53:04 +00001433 is_nothrow_move_constructible<__node_allocator>::value &&
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001434 is_nothrow_move_constructible<hasher>::value &&
1435 is_nothrow_move_constructible<key_equal>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001436 : __bucket_list_(_VSTD::move(__u.__bucket_list_)),
1437 __p1_(_VSTD::move(__u.__p1_)),
1438 __p2_(_VSTD::move(__u.__p2_)),
1439 __p3_(_VSTD::move(__u.__p3_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001440{
1441 if (size() > 0)
1442 {
Howard Hinnant7a445152012-07-06 17:31:14 +00001443 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash_, bucket_count())] =
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001444 static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001445 __u.__p1_.first().__next_ = nullptr;
1446 __u.size() = 0;
1447 }
1448}
1449
1450template <class _Tp, class _Hash, class _Equal, class _Alloc>
1451__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(__hash_table&& __u,
1452 const allocator_type& __a)
1453 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1454 __p1_(__node_allocator(__a)),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001455 __p2_(0, _VSTD::move(__u.hash_function())),
1456 __p3_(_VSTD::move(__u.__p3_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001457{
1458 if (__a == allocator_type(__u.__node_alloc()))
1459 {
1460 __bucket_list_.reset(__u.__bucket_list_.release());
1461 __bucket_list_.get_deleter().size() = __u.__bucket_list_.get_deleter().size();
1462 __u.__bucket_list_.get_deleter().size() = 0;
1463 if (__u.size() > 0)
1464 {
1465 __p1_.first().__next_ = __u.__p1_.first().__next_;
1466 __u.__p1_.first().__next_ = nullptr;
Howard Hinnant7a445152012-07-06 17:31:14 +00001467 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash_, bucket_count())] =
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001468 static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001469 size() = __u.size();
1470 __u.size() = 0;
1471 }
1472 }
1473}
1474
Eric Fiselier2960ae22016-02-11 11:59:44 +00001475#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001476
1477template <class _Tp, class _Hash, class _Equal, class _Alloc>
1478__hash_table<_Tp, _Hash, _Equal, _Alloc>::~__hash_table()
1479{
1480 __deallocate(__p1_.first().__next_);
Howard Hinnant39213642013-07-23 22:01:58 +00001481#if _LIBCPP_DEBUG_LEVEL >= 2
1482 __get_db()->__erase_c(this);
1483#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001484}
1485
1486template <class _Tp, class _Hash, class _Equal, class _Alloc>
1487void
1488__hash_table<_Tp, _Hash, _Equal, _Alloc>::__copy_assign_alloc(
1489 const __hash_table& __u, true_type)
1490{
1491 if (__node_alloc() != __u.__node_alloc())
1492 {
1493 clear();
1494 __bucket_list_.reset();
1495 __bucket_list_.get_deleter().size() = 0;
1496 }
1497 __bucket_list_.get_deleter().__alloc() = __u.__bucket_list_.get_deleter().__alloc();
1498 __node_alloc() = __u.__node_alloc();
1499}
1500
1501template <class _Tp, class _Hash, class _Equal, class _Alloc>
1502__hash_table<_Tp, _Hash, _Equal, _Alloc>&
1503__hash_table<_Tp, _Hash, _Equal, _Alloc>::operator=(const __hash_table& __u)
1504{
1505 if (this != &__u)
1506 {
1507 __copy_assign_alloc(__u);
1508 hash_function() = __u.hash_function();
1509 key_eq() = __u.key_eq();
1510 max_load_factor() = __u.max_load_factor();
1511 __assign_multi(__u.begin(), __u.end());
1512 }
1513 return *this;
1514}
1515
1516template <class _Tp, class _Hash, class _Equal, class _Alloc>
1517void
1518__hash_table<_Tp, _Hash, _Equal, _Alloc>::__deallocate(__node_pointer __np)
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001519 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001520{
1521 __node_allocator& __na = __node_alloc();
1522 while (__np != nullptr)
1523 {
1524 __node_pointer __next = __np->__next_;
Howard Hinnant39213642013-07-23 22:01:58 +00001525#if _LIBCPP_DEBUG_LEVEL >= 2
1526 __c_node* __c = __get_db()->__find_c_and_lock(this);
1527 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1528 {
1529 --__p;
1530 iterator* __i = static_cast<iterator*>((*__p)->__i_);
1531 if (__i->__node_ == __np)
1532 {
1533 (*__p)->__c_ = nullptr;
1534 if (--__c->end_ != __p)
1535 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1536 }
1537 }
1538 __get_db()->unlock();
1539#endif
Eric Fiselier2960ae22016-02-11 11:59:44 +00001540 __node_traits::destroy(__na, _NodeTypes::__get_ptr(__np->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001541 __node_traits::deallocate(__na, __np, 1);
1542 __np = __next;
1543 }
1544}
1545
1546template <class _Tp, class _Hash, class _Equal, class _Alloc>
1547typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_pointer
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001548__hash_table<_Tp, _Hash, _Equal, _Alloc>::__detach() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001549{
1550 size_type __bc = bucket_count();
1551 for (size_type __i = 0; __i < __bc; ++__i)
1552 __bucket_list_[__i] = nullptr;
1553 size() = 0;
1554 __node_pointer __cache = __p1_.first().__next_;
1555 __p1_.first().__next_ = nullptr;
1556 return __cache;
1557}
1558
Eric Fiselier2960ae22016-02-11 11:59:44 +00001559#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001560
1561template <class _Tp, class _Hash, class _Equal, class _Alloc>
1562void
1563__hash_table<_Tp, _Hash, _Equal, _Alloc>::__move_assign(
1564 __hash_table& __u, true_type)
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001565 _NOEXCEPT_(
1566 is_nothrow_move_assignable<__node_allocator>::value &&
1567 is_nothrow_move_assignable<hasher>::value &&
1568 is_nothrow_move_assignable<key_equal>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001569{
1570 clear();
1571 __bucket_list_.reset(__u.__bucket_list_.release());
1572 __bucket_list_.get_deleter().size() = __u.__bucket_list_.get_deleter().size();
1573 __u.__bucket_list_.get_deleter().size() = 0;
1574 __move_assign_alloc(__u);
1575 size() = __u.size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001576 hash_function() = _VSTD::move(__u.hash_function());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001577 max_load_factor() = __u.max_load_factor();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001578 key_eq() = _VSTD::move(__u.key_eq());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001579 __p1_.first().__next_ = __u.__p1_.first().__next_;
1580 if (size() > 0)
1581 {
Howard Hinnant7a445152012-07-06 17:31:14 +00001582 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash_, bucket_count())] =
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001583 static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001584 __u.__p1_.first().__next_ = nullptr;
1585 __u.size() = 0;
1586 }
Howard Hinnant39213642013-07-23 22:01:58 +00001587#if _LIBCPP_DEBUG_LEVEL >= 2
1588 __get_db()->swap(this, &__u);
1589#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001590}
1591
1592template <class _Tp, class _Hash, class _Equal, class _Alloc>
1593void
1594__hash_table<_Tp, _Hash, _Equal, _Alloc>::__move_assign(
1595 __hash_table& __u, false_type)
1596{
1597 if (__node_alloc() == __u.__node_alloc())
1598 __move_assign(__u, true_type());
1599 else
1600 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001601 hash_function() = _VSTD::move(__u.hash_function());
1602 key_eq() = _VSTD::move(__u.key_eq());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001603 max_load_factor() = __u.max_load_factor();
1604 if (bucket_count() != 0)
1605 {
1606 __node_pointer __cache = __detach();
1607#ifndef _LIBCPP_NO_EXCEPTIONS
1608 try
1609 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001610#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001611 const_iterator __i = __u.begin();
1612 while (__cache != nullptr && __u.size() != 0)
1613 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001614 __cache->__value_ = _VSTD::move(__u.remove(__i++)->__value_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001615 __node_pointer __next = __cache->__next_;
1616 __node_insert_multi(__cache);
1617 __cache = __next;
1618 }
1619#ifndef _LIBCPP_NO_EXCEPTIONS
1620 }
1621 catch (...)
1622 {
1623 __deallocate(__cache);
1624 throw;
1625 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001626#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001627 __deallocate(__cache);
1628 }
1629 const_iterator __i = __u.begin();
1630 while (__u.size() != 0)
1631 {
Eric Fiselier2960ae22016-02-11 11:59:44 +00001632 __node_holder __h = __construct_node(_NodeTypes::__move(__u.remove(__i++)->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001633 __node_insert_multi(__h.get());
1634 __h.release();
1635 }
1636 }
1637}
1638
1639template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001640inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001641__hash_table<_Tp, _Hash, _Equal, _Alloc>&
1642__hash_table<_Tp, _Hash, _Equal, _Alloc>::operator=(__hash_table&& __u)
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001643 _NOEXCEPT_(
1644 __node_traits::propagate_on_container_move_assignment::value &&
1645 is_nothrow_move_assignable<__node_allocator>::value &&
1646 is_nothrow_move_assignable<hasher>::value &&
1647 is_nothrow_move_assignable<key_equal>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001648{
1649 __move_assign(__u, integral_constant<bool,
1650 __node_traits::propagate_on_container_move_assignment::value>());
1651 return *this;
1652}
1653
Eric Fiselier2960ae22016-02-11 11:59:44 +00001654#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001655
1656template <class _Tp, class _Hash, class _Equal, class _Alloc>
1657template <class _InputIterator>
1658void
1659__hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_unique(_InputIterator __first,
1660 _InputIterator __last)
1661{
Eric Fiselier2960ae22016-02-11 11:59:44 +00001662 typedef iterator_traits<_InputIterator> _ITraits;
1663 typedef typename _ITraits::value_type _ItValueType;
1664 static_assert((is_same<_ItValueType, __container_value_type>::value),
1665 "__assign_unique may only be called with the containers value type");
1666
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001667 if (bucket_count() != 0)
1668 {
1669 __node_pointer __cache = __detach();
1670#ifndef _LIBCPP_NO_EXCEPTIONS
1671 try
1672 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001673#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001674 for (; __cache != nullptr && __first != __last; ++__first)
1675 {
1676 __cache->__value_ = *__first;
1677 __node_pointer __next = __cache->__next_;
1678 __node_insert_unique(__cache);
1679 __cache = __next;
1680 }
1681#ifndef _LIBCPP_NO_EXCEPTIONS
1682 }
1683 catch (...)
1684 {
1685 __deallocate(__cache);
1686 throw;
1687 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001688#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001689 __deallocate(__cache);
1690 }
1691 for (; __first != __last; ++__first)
1692 __insert_unique(*__first);
1693}
1694
1695template <class _Tp, class _Hash, class _Equal, class _Alloc>
1696template <class _InputIterator>
1697void
1698__hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_multi(_InputIterator __first,
1699 _InputIterator __last)
1700{
Eric Fiselier2960ae22016-02-11 11:59:44 +00001701 typedef iterator_traits<_InputIterator> _ITraits;
1702 typedef typename _ITraits::value_type _ItValueType;
1703 static_assert((is_same<_ItValueType, __container_value_type>::value ||
1704 is_same<_ItValueType, __node_value_type>::value),
1705 "__assign_multi may only be called with the containers value type"
1706 " or the nodes value type");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001707 if (bucket_count() != 0)
1708 {
1709 __node_pointer __cache = __detach();
1710#ifndef _LIBCPP_NO_EXCEPTIONS
1711 try
1712 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001713#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001714 for (; __cache != nullptr && __first != __last; ++__first)
1715 {
1716 __cache->__value_ = *__first;
1717 __node_pointer __next = __cache->__next_;
1718 __node_insert_multi(__cache);
1719 __cache = __next;
1720 }
1721#ifndef _LIBCPP_NO_EXCEPTIONS
1722 }
1723 catch (...)
1724 {
1725 __deallocate(__cache);
1726 throw;
1727 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001728#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001729 __deallocate(__cache);
1730 }
1731 for (; __first != __last; ++__first)
Eric Fiselier2960ae22016-02-11 11:59:44 +00001732 __insert_multi(_NodeTypes::__get_value(*__first));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001733}
1734
1735template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001736inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001737typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001738__hash_table<_Tp, _Hash, _Equal, _Alloc>::begin() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001739{
Howard Hinnant39213642013-07-23 22:01:58 +00001740#if _LIBCPP_DEBUG_LEVEL >= 2
1741 return iterator(__p1_.first().__next_, this);
1742#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001743 return iterator(__p1_.first().__next_);
Howard Hinnant39213642013-07-23 22:01:58 +00001744#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001745}
1746
1747template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001748inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001749typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001750__hash_table<_Tp, _Hash, _Equal, _Alloc>::end() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001751{
Howard Hinnant39213642013-07-23 22:01:58 +00001752#if _LIBCPP_DEBUG_LEVEL >= 2
1753 return iterator(nullptr, this);
1754#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001755 return iterator(nullptr);
Howard Hinnant39213642013-07-23 22:01:58 +00001756#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001757}
1758
1759template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001760inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001761typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001762__hash_table<_Tp, _Hash, _Equal, _Alloc>::begin() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001763{
Howard Hinnant39213642013-07-23 22:01:58 +00001764#if _LIBCPP_DEBUG_LEVEL >= 2
1765 return const_iterator(__p1_.first().__next_, this);
1766#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001767 return const_iterator(__p1_.first().__next_);
Howard Hinnant39213642013-07-23 22:01:58 +00001768#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001769}
1770
1771template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001772inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001773typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001774__hash_table<_Tp, _Hash, _Equal, _Alloc>::end() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001775{
Howard Hinnant39213642013-07-23 22:01:58 +00001776#if _LIBCPP_DEBUG_LEVEL >= 2
1777 return const_iterator(nullptr, this);
1778#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001779 return const_iterator(nullptr);
Howard Hinnant39213642013-07-23 22:01:58 +00001780#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001781}
1782
1783template <class _Tp, class _Hash, class _Equal, class _Alloc>
1784void
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001785__hash_table<_Tp, _Hash, _Equal, _Alloc>::clear() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001786{
1787 if (size() > 0)
1788 {
1789 __deallocate(__p1_.first().__next_);
1790 __p1_.first().__next_ = nullptr;
1791 size_type __bc = bucket_count();
Howard Hinnant9f66bff2011-07-05 14:14:17 +00001792 for (size_type __i = 0; __i < __bc; ++__i)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001793 __bucket_list_[__i] = nullptr;
1794 size() = 0;
1795 }
1796}
1797
1798template <class _Tp, class _Hash, class _Equal, class _Alloc>
1799pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
1800__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_unique(__node_pointer __nd)
1801{
1802 __nd->__hash_ = hash_function()(__nd->__value_);
1803 size_type __bc = bucket_count();
1804 bool __inserted = false;
1805 __node_pointer __ndptr;
1806 size_t __chash;
1807 if (__bc != 0)
1808 {
Howard Hinnant7a445152012-07-06 17:31:14 +00001809 __chash = __constrain_hash(__nd->__hash_, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001810 __ndptr = __bucket_list_[__chash];
1811 if (__ndptr != nullptr)
1812 {
1813 for (__ndptr = __ndptr->__next_; __ndptr != nullptr &&
Howard Hinnant7a445152012-07-06 17:31:14 +00001814 __constrain_hash(__ndptr->__hash_, __bc) == __chash;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001815 __ndptr = __ndptr->__next_)
1816 {
1817 if (key_eq()(__ndptr->__value_, __nd->__value_))
1818 goto __done;
1819 }
1820 }
1821 }
1822 {
1823 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1824 {
Eric Fiselier57947ca2015-02-02 21:31:48 +00001825 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001826 size_type(ceil(float(size() + 1) / max_load_factor()))));
1827 __bc = bucket_count();
Howard Hinnant7a445152012-07-06 17:31:14 +00001828 __chash = __constrain_hash(__nd->__hash_, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001829 }
1830 // insert_after __bucket_list_[__chash], or __first_node if bucket is null
1831 __node_pointer __pn = __bucket_list_[__chash];
1832 if (__pn == nullptr)
1833 {
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001834 __pn = static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001835 __nd->__next_ = __pn->__next_;
1836 __pn->__next_ = __nd;
1837 // fix up __bucket_list_
1838 __bucket_list_[__chash] = __pn;
1839 if (__nd->__next_ != nullptr)
Howard Hinnant7a445152012-07-06 17:31:14 +00001840 __bucket_list_[__constrain_hash(__nd->__next_->__hash_, __bc)] = __nd;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001841 }
1842 else
1843 {
1844 __nd->__next_ = __pn->__next_;
1845 __pn->__next_ = __nd;
1846 }
1847 __ndptr = __nd;
1848 // increment size
1849 ++size();
1850 __inserted = true;
1851 }
1852__done:
Howard Hinnant39213642013-07-23 22:01:58 +00001853#if _LIBCPP_DEBUG_LEVEL >= 2
1854 return pair<iterator, bool>(iterator(__ndptr, this), __inserted);
1855#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001856 return pair<iterator, bool>(iterator(__ndptr), __inserted);
Howard Hinnant39213642013-07-23 22:01:58 +00001857#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001858}
1859
1860template <class _Tp, class _Hash, class _Equal, class _Alloc>
1861typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1862__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi(__node_pointer __cp)
1863{
1864 __cp->__hash_ = hash_function()(__cp->__value_);
1865 size_type __bc = bucket_count();
1866 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1867 {
Eric Fiselier57947ca2015-02-02 21:31:48 +00001868 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001869 size_type(ceil(float(size() + 1) / max_load_factor()))));
1870 __bc = bucket_count();
1871 }
Howard Hinnant7a445152012-07-06 17:31:14 +00001872 size_t __chash = __constrain_hash(__cp->__hash_, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001873 __node_pointer __pn = __bucket_list_[__chash];
1874 if (__pn == nullptr)
1875 {
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001876 __pn = static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001877 __cp->__next_ = __pn->__next_;
1878 __pn->__next_ = __cp;
1879 // fix up __bucket_list_
1880 __bucket_list_[__chash] = __pn;
1881 if (__cp->__next_ != nullptr)
Howard Hinnant7a445152012-07-06 17:31:14 +00001882 __bucket_list_[__constrain_hash(__cp->__next_->__hash_, __bc)] = __cp;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001883 }
1884 else
1885 {
1886 for (bool __found = false; __pn->__next_ != nullptr &&
Howard Hinnant7a445152012-07-06 17:31:14 +00001887 __constrain_hash(__pn->__next_->__hash_, __bc) == __chash;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001888 __pn = __pn->__next_)
Howard Hinnant324bb032010-08-22 00:02:43 +00001889 {
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001890 // __found key_eq() action
1891 // false false loop
1892 // true true loop
1893 // false true set __found to true
1894 // true false break
1895 if (__found != (__pn->__next_->__hash_ == __cp->__hash_ &&
1896 key_eq()(__pn->__next_->__value_, __cp->__value_)))
1897 {
1898 if (!__found)
1899 __found = true;
1900 else
1901 break;
1902 }
1903 }
1904 __cp->__next_ = __pn->__next_;
1905 __pn->__next_ = __cp;
1906 if (__cp->__next_ != nullptr)
1907 {
Howard Hinnant7a445152012-07-06 17:31:14 +00001908 size_t __nhash = __constrain_hash(__cp->__next_->__hash_, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001909 if (__nhash != __chash)
1910 __bucket_list_[__nhash] = __cp;
1911 }
1912 }
1913 ++size();
Howard Hinnant39213642013-07-23 22:01:58 +00001914#if _LIBCPP_DEBUG_LEVEL >= 2
1915 return iterator(__cp, this);
1916#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001917 return iterator(__cp);
Howard Hinnant39213642013-07-23 22:01:58 +00001918#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001919}
1920
1921template <class _Tp, class _Hash, class _Equal, class _Alloc>
1922typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1923__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi(
1924 const_iterator __p, __node_pointer __cp)
1925{
Howard Hinnant824c1992013-08-02 17:50:49 +00001926#if _LIBCPP_DEBUG_LEVEL >= 2
1927 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1928 "unordered container::emplace_hint(const_iterator, args...) called with an iterator not"
1929 " referring to this unordered container");
1930#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001931 if (__p != end() && key_eq()(*__p, __cp->__value_))
1932 {
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001933 __node_pointer __np = __p.__node_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001934 __cp->__hash_ = __np->__hash_;
1935 size_type __bc = bucket_count();
1936 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1937 {
Eric Fiselier57947ca2015-02-02 21:31:48 +00001938 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001939 size_type(ceil(float(size() + 1) / max_load_factor()))));
1940 __bc = bucket_count();
1941 }
Howard Hinnant7a445152012-07-06 17:31:14 +00001942 size_t __chash = __constrain_hash(__cp->__hash_, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001943 __node_pointer __pp = __bucket_list_[__chash];
1944 while (__pp->__next_ != __np)
1945 __pp = __pp->__next_;
1946 __cp->__next_ = __np;
1947 __pp->__next_ = __cp;
1948 ++size();
Howard Hinnant39213642013-07-23 22:01:58 +00001949#if _LIBCPP_DEBUG_LEVEL >= 2
1950 return iterator(__cp, this);
1951#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001952 return iterator(__cp);
Howard Hinnant39213642013-07-23 22:01:58 +00001953#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001954 }
1955 return __node_insert_multi(__cp);
1956}
1957
Eric Fiselierfdae69a2015-06-13 07:18:32 +00001958
1959
Eric Fiselier2960ae22016-02-11 11:59:44 +00001960#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierfdae69a2015-06-13 07:18:32 +00001961template <class _Tp, class _Hash, class _Equal, class _Alloc>
Eric Fiselier2960ae22016-02-11 11:59:44 +00001962template <class _Key, class ..._Args>
Eric Fiselierfdae69a2015-06-13 07:18:32 +00001963_LIBCPP_INLINE_VISIBILITY
1964pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
Eric Fiselier2960ae22016-02-11 11:59:44 +00001965__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_key_args(_Key const& __k, _Args&&... __args)
Eric Fiselierfdae69a2015-06-13 07:18:32 +00001966#else
1967template <class _Tp, class _Hash, class _Equal, class _Alloc>
Eric Fiselier2960ae22016-02-11 11:59:44 +00001968template <class _Key, class _Args>
Eric Fiselierfdae69a2015-06-13 07:18:32 +00001969_LIBCPP_INLINE_VISIBILITY
1970pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
Eric Fiselier2960ae22016-02-11 11:59:44 +00001971__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_key_args(_Key const& __k, _Args& __args)
Eric Fiselierfdae69a2015-06-13 07:18:32 +00001972#endif
1973{
Eric Fiselier2960ae22016-02-11 11:59:44 +00001974
1975 size_t __hash = hash_function()(__k);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001976 size_type __bc = bucket_count();
1977 bool __inserted = false;
1978 __node_pointer __nd;
1979 size_t __chash;
1980 if (__bc != 0)
1981 {
Howard Hinnant7a445152012-07-06 17:31:14 +00001982 __chash = __constrain_hash(__hash, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001983 __nd = __bucket_list_[__chash];
1984 if (__nd != nullptr)
1985 {
1986 for (__nd = __nd->__next_; __nd != nullptr &&
Howard Hinnant7a445152012-07-06 17:31:14 +00001987 __constrain_hash(__nd->__hash_, __bc) == __chash;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001988 __nd = __nd->__next_)
1989 {
Eric Fiselier2960ae22016-02-11 11:59:44 +00001990 if (key_eq()(__nd->__value_, __k))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001991 goto __done;
1992 }
1993 }
1994 }
1995 {
Eric Fiselier2960ae22016-02-11 11:59:44 +00001996#ifndef _LIBCPP_CXX03_LANG
1997 __node_holder __h = __construct_node_hash(__hash, _VSTD::forward<_Args>(__args)...);
1998#else
1999 __node_holder __h = __construct_node_hash(__hash, __args);
2000#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002001 if (size()+1 > __bc * max_load_factor() || __bc == 0)
2002 {
Eric Fiselier57947ca2015-02-02 21:31:48 +00002003 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002004 size_type(ceil(float(size() + 1) / max_load_factor()))));
2005 __bc = bucket_count();
Howard Hinnant7a445152012-07-06 17:31:14 +00002006 __chash = __constrain_hash(__hash, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002007 }
2008 // insert_after __bucket_list_[__chash], or __first_node if bucket is null
2009 __node_pointer __pn = __bucket_list_[__chash];
2010 if (__pn == nullptr)
2011 {
Eric Fiselier9e9f42e2016-02-11 15:22:37 +00002012 __pn = static_cast<__node_pointer>(static_cast<__void_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first())));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002013 __h->__next_ = __pn->__next_;
2014 __pn->__next_ = __h.get();
2015 // fix up __bucket_list_
2016 __bucket_list_[__chash] = __pn;
2017 if (__h->__next_ != nullptr)
Howard Hinnant7a445152012-07-06 17:31:14 +00002018 __bucket_list_[__constrain_hash(__h->__next_->__hash_, __bc)] = __h.get();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002019 }
2020 else
2021 {
2022 __h->__next_ = __pn->__next_;
2023 __pn->__next_ = __h.get();
2024 }
2025 __nd = __h.release();
2026 // increment size
2027 ++size();
2028 __inserted = true;
2029 }
2030__done:
Howard Hinnant39213642013-07-23 22:01:58 +00002031#if _LIBCPP_DEBUG_LEVEL >= 2
2032 return pair<iterator, bool>(iterator(__nd, this), __inserted);
2033#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002034 return pair<iterator, bool>(iterator(__nd), __inserted);
Howard Hinnant39213642013-07-23 22:01:58 +00002035#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002036}
2037
Eric Fiselier2960ae22016-02-11 11:59:44 +00002038#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002039
2040template <class _Tp, class _Hash, class _Equal, class _Alloc>
2041template <class... _Args>
2042pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
Duncan P. N. Exon Smith798ec842016-03-17 20:45:20 +00002043__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_impl(_Args&&... __args)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002044{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002045 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002046 pair<iterator, bool> __r = __node_insert_unique(__h.get());
2047 if (__r.second)
2048 __h.release();
2049 return __r;
2050}
2051
2052template <class _Tp, class _Hash, class _Equal, class _Alloc>
2053template <class... _Args>
2054typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2055__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_multi(_Args&&... __args)
2056{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002057 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002058 iterator __r = __node_insert_multi(__h.get());
2059 __h.release();
2060 return __r;
2061}
2062
2063template <class _Tp, class _Hash, class _Equal, class _Alloc>
2064template <class... _Args>
2065typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2066__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_hint_multi(
2067 const_iterator __p, _Args&&... __args)
2068{
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00002069#if _LIBCPP_DEBUG_LEVEL >= 2
2070 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2071 "unordered container::emplace_hint(const_iterator, args...) called with an iterator not"
2072 " referring to this unordered container");
2073#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00002074 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002075 iterator __r = __node_insert_multi(__p, __h.get());
2076 __h.release();
2077 return __r;
2078}
2079
Eric Fiselier2960ae22016-02-11 11:59:44 +00002080#else // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002081
2082template <class _Tp, class _Hash, class _Equal, class _Alloc>
2083typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
Eric Fiselier2960ae22016-02-11 11:59:44 +00002084__hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const __container_value_type& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002085{
2086 __node_holder __h = __construct_node(__x);
2087 iterator __r = __node_insert_multi(__h.get());
2088 __h.release();
2089 return __r;
2090}
2091
2092template <class _Tp, class _Hash, class _Equal, class _Alloc>
2093typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2094__hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const_iterator __p,
Eric Fiselier2960ae22016-02-11 11:59:44 +00002095 const __container_value_type& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002096{
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00002097#if _LIBCPP_DEBUG_LEVEL >= 2
2098 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2099 "unordered container::insert(const_iterator, lvalue) called with an iterator not"
2100 " referring to this unordered container");
2101#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002102 __node_holder __h = __construct_node(__x);
2103 iterator __r = __node_insert_multi(__p, __h.get());
2104 __h.release();
2105 return __r;
2106}
2107
Eric Fiselier2960ae22016-02-11 11:59:44 +00002108#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002109
2110template <class _Tp, class _Hash, class _Equal, class _Alloc>
2111void
2112__hash_table<_Tp, _Hash, _Equal, _Alloc>::rehash(size_type __n)
2113{
Howard Hinnant7a445152012-07-06 17:31:14 +00002114 if (__n == 1)
2115 __n = 2;
2116 else if (__n & (__n - 1))
2117 __n = __next_prime(__n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002118 size_type __bc = bucket_count();
2119 if (__n > __bc)
2120 __rehash(__n);
Howard Hinnant7a445152012-07-06 17:31:14 +00002121 else if (__n < __bc)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002122 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002123 __n = _VSTD::max<size_type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002124 (
2125 __n,
Eric Fiselier57947ca2015-02-02 21:31:48 +00002126 __is_hash_power2(__bc) ? __next_hash_pow2(size_t(ceil(float(size()) / max_load_factor()))) :
2127 __next_prime(size_t(ceil(float(size()) / max_load_factor())))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002128 );
2129 if (__n < __bc)
2130 __rehash(__n);
2131 }
2132}
2133
2134template <class _Tp, class _Hash, class _Equal, class _Alloc>
2135void
2136__hash_table<_Tp, _Hash, _Equal, _Alloc>::__rehash(size_type __nbc)
2137{
Howard Hinnant39213642013-07-23 22:01:58 +00002138#if _LIBCPP_DEBUG_LEVEL >= 2
2139 __get_db()->__invalidate_all(this);
2140#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002141 __pointer_allocator& __npa = __bucket_list_.get_deleter().__alloc();
2142 __bucket_list_.reset(__nbc > 0 ?
2143 __pointer_alloc_traits::allocate(__npa, __nbc) : nullptr);
2144 __bucket_list_.get_deleter().size() = __nbc;
2145 if (__nbc > 0)
2146 {
2147 for (size_type __i = 0; __i < __nbc; ++__i)
2148 __bucket_list_[__i] = nullptr;
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00002149 __node_pointer __pp(static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first())));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002150 __node_pointer __cp = __pp->__next_;
2151 if (__cp != nullptr)
2152 {
Howard Hinnant7a445152012-07-06 17:31:14 +00002153 size_type __chash = __constrain_hash(__cp->__hash_, __nbc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002154 __bucket_list_[__chash] = __pp;
2155 size_type __phash = __chash;
2156 for (__pp = __cp, __cp = __cp->__next_; __cp != nullptr;
2157 __cp = __pp->__next_)
2158 {
Howard Hinnant7a445152012-07-06 17:31:14 +00002159 __chash = __constrain_hash(__cp->__hash_, __nbc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002160 if (__chash == __phash)
2161 __pp = __cp;
2162 else
2163 {
2164 if (__bucket_list_[__chash] == nullptr)
2165 {
2166 __bucket_list_[__chash] = __pp;
2167 __pp = __cp;
2168 __phash = __chash;
2169 }
2170 else
2171 {
2172 __node_pointer __np = __cp;
2173 for (; __np->__next_ != nullptr &&
2174 key_eq()(__cp->__value_, __np->__next_->__value_);
2175 __np = __np->__next_)
2176 ;
2177 __pp->__next_ = __np->__next_;
2178 __np->__next_ = __bucket_list_[__chash]->__next_;
2179 __bucket_list_[__chash]->__next_ = __cp;
Howard Hinnant324bb032010-08-22 00:02:43 +00002180
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002181 }
2182 }
2183 }
2184 }
2185 }
2186}
2187
2188template <class _Tp, class _Hash, class _Equal, class _Alloc>
2189template <class _Key>
2190typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2191__hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k)
2192{
2193 size_t __hash = hash_function()(__k);
2194 size_type __bc = bucket_count();
2195 if (__bc != 0)
2196 {
Howard Hinnant7a445152012-07-06 17:31:14 +00002197 size_t __chash = __constrain_hash(__hash, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002198 __node_pointer __nd = __bucket_list_[__chash];
2199 if (__nd != nullptr)
2200 {
2201 for (__nd = __nd->__next_; __nd != nullptr &&
Howard Hinnant7a445152012-07-06 17:31:14 +00002202 __constrain_hash(__nd->__hash_, __bc) == __chash;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002203 __nd = __nd->__next_)
2204 {
2205 if (key_eq()(__nd->__value_, __k))
Howard Hinnant39213642013-07-23 22:01:58 +00002206#if _LIBCPP_DEBUG_LEVEL >= 2
2207 return iterator(__nd, this);
2208#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002209 return iterator(__nd);
Howard Hinnant39213642013-07-23 22:01:58 +00002210#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002211 }
2212 }
2213 }
2214 return end();
2215}
2216
2217template <class _Tp, class _Hash, class _Equal, class _Alloc>
2218template <class _Key>
2219typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
2220__hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k) const
2221{
2222 size_t __hash = hash_function()(__k);
2223 size_type __bc = bucket_count();
2224 if (__bc != 0)
2225 {
Howard Hinnant7a445152012-07-06 17:31:14 +00002226 size_t __chash = __constrain_hash(__hash, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002227 __node_const_pointer __nd = __bucket_list_[__chash];
2228 if (__nd != nullptr)
2229 {
2230 for (__nd = __nd->__next_; __nd != nullptr &&
Howard Hinnant7a445152012-07-06 17:31:14 +00002231 __constrain_hash(__nd->__hash_, __bc) == __chash;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002232 __nd = __nd->__next_)
2233 {
2234 if (key_eq()(__nd->__value_, __k))
Howard Hinnant39213642013-07-23 22:01:58 +00002235#if _LIBCPP_DEBUG_LEVEL >= 2
2236 return const_iterator(__nd, this);
2237#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002238 return const_iterator(__nd);
Howard Hinnant39213642013-07-23 22:01:58 +00002239#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002240 }
2241 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002242
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002243 }
2244 return end();
2245}
2246
Eric Fiselier2960ae22016-02-11 11:59:44 +00002247#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002248
2249template <class _Tp, class _Hash, class _Equal, class _Alloc>
2250template <class ..._Args>
2251typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
2252__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(_Args&& ...__args)
2253{
Eric Fiselier2960ae22016-02-11 11:59:44 +00002254 static_assert(!__is_hash_value_type<_Args...>::value,
2255 "Construct cannot be called with a hash value type");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002256 __node_allocator& __na = __node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00002257 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselier2960ae22016-02-11 11:59:44 +00002258 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002259 __h.get_deleter().__value_constructed = true;
2260 __h->__hash_ = hash_function()(__h->__value_);
2261 __h->__next_ = nullptr;
2262 return __h;
2263}
2264
2265template <class _Tp, class _Hash, class _Equal, class _Alloc>
Eric Fiselier2960ae22016-02-11 11:59:44 +00002266template <class _First, class ..._Rest>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002267typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Eric Fiselier2960ae22016-02-11 11:59:44 +00002268__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node_hash(
2269 size_t __hash, _First&& __f, _Rest&& ...__rest)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002270{
Eric Fiselier2960ae22016-02-11 11:59:44 +00002271 static_assert(!__is_hash_value_type<_First, _Rest...>::value,
2272 "Construct cannot be called with a hash value type");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002273 __node_allocator& __na = __node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00002274 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselier2960ae22016-02-11 11:59:44 +00002275 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_),
2276 _VSTD::forward<_First>(__f),
2277 _VSTD::forward<_Rest>(__rest)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002278 __h.get_deleter().__value_constructed = true;
2279 __h->__hash_ = __hash;
2280 __h->__next_ = nullptr;
Howard Hinnant9a894d92013-08-22 18:29:50 +00002281 return __h;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002282}
2283
Eric Fiselier2960ae22016-02-11 11:59:44 +00002284#else // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002285
2286template <class _Tp, class _Hash, class _Equal, class _Alloc>
2287typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Eric Fiselier2960ae22016-02-11 11:59:44 +00002288__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(const __container_value_type& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002289{
2290 __node_allocator& __na = __node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00002291 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselier2960ae22016-02-11 11:59:44 +00002292 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002293 __h.get_deleter().__value_constructed = true;
2294 __h->__hash_ = hash_function()(__h->__value_);
2295 __h->__next_ = nullptr;
Dimitry Andric89663502015-08-19 06:43:33 +00002296 return _LIBCPP_EXPLICIT_MOVE(__h); // explicitly moved for C++03
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002297}
2298
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002299template <class _Tp, class _Hash, class _Equal, class _Alloc>
2300typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Eric Fiselier2960ae22016-02-11 11:59:44 +00002301__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node_hash(size_t __hash,
2302 const __container_value_type& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002303{
2304 __node_allocator& __na = __node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00002305 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselier2960ae22016-02-11 11:59:44 +00002306 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002307 __h.get_deleter().__value_constructed = true;
2308 __h->__hash_ = __hash;
2309 __h->__next_ = nullptr;
Dimitry Andric89663502015-08-19 06:43:33 +00002310 return _LIBCPP_EXPLICIT_MOVE(__h); // explicitly moved for C++03
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002311}
2312
Eric Fiselier2960ae22016-02-11 11:59:44 +00002313#endif // _LIBCPP_CXX03_LANG
2314
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002315template <class _Tp, class _Hash, class _Equal, class _Alloc>
2316typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2317__hash_table<_Tp, _Hash, _Equal, _Alloc>::erase(const_iterator __p)
2318{
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00002319 __node_pointer __np = __p.__node_;
Howard Hinnant39213642013-07-23 22:01:58 +00002320#if _LIBCPP_DEBUG_LEVEL >= 2
2321 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2322 "unordered container erase(iterator) called with an iterator not"
2323 " referring to this container");
2324 _LIBCPP_ASSERT(__p != end(),
2325 "unordered container erase(iterator) called with a non-dereferenceable iterator");
2326 iterator __r(__np, this);
2327#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002328 iterator __r(__np);
Howard Hinnant39213642013-07-23 22:01:58 +00002329#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002330 ++__r;
2331 remove(__p);
2332 return __r;
2333}
2334
2335template <class _Tp, class _Hash, class _Equal, class _Alloc>
2336typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2337__hash_table<_Tp, _Hash, _Equal, _Alloc>::erase(const_iterator __first,
2338 const_iterator __last)
2339{
Howard Hinnant39213642013-07-23 22:01:58 +00002340#if _LIBCPP_DEBUG_LEVEL >= 2
2341 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
2342 "unodered container::erase(iterator, iterator) called with an iterator not"
2343 " referring to this unodered container");
2344 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__last) == this,
2345 "unodered container::erase(iterator, iterator) called with an iterator not"
2346 " referring to this unodered container");
2347#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002348 for (const_iterator __p = __first; __first != __last; __p = __first)
2349 {
2350 ++__first;
2351 erase(__p);
2352 }
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00002353 __node_pointer __np = __last.__node_;
Howard Hinnant39213642013-07-23 22:01:58 +00002354#if _LIBCPP_DEBUG_LEVEL >= 2
2355 return iterator (__np, this);
2356#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002357 return iterator (__np);
Howard Hinnant39213642013-07-23 22:01:58 +00002358#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002359}
2360
2361template <class _Tp, class _Hash, class _Equal, class _Alloc>
2362template <class _Key>
2363typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2364__hash_table<_Tp, _Hash, _Equal, _Alloc>::__erase_unique(const _Key& __k)
2365{
2366 iterator __i = find(__k);
2367 if (__i == end())
2368 return 0;
2369 erase(__i);
2370 return 1;
2371}
2372
2373template <class _Tp, class _Hash, class _Equal, class _Alloc>
2374template <class _Key>
2375typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2376__hash_table<_Tp, _Hash, _Equal, _Alloc>::__erase_multi(const _Key& __k)
2377{
2378 size_type __r = 0;
2379 iterator __i = find(__k);
2380 if (__i != end())
2381 {
2382 iterator __e = end();
2383 do
2384 {
2385 erase(__i++);
2386 ++__r;
2387 } while (__i != __e && key_eq()(*__i, __k));
2388 }
2389 return __r;
2390}
2391
2392template <class _Tp, class _Hash, class _Equal, class _Alloc>
2393typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00002394__hash_table<_Tp, _Hash, _Equal, _Alloc>::remove(const_iterator __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002395{
2396 // current node
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00002397 __node_pointer __cn = __p.__node_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002398 size_type __bc = bucket_count();
Howard Hinnant7a445152012-07-06 17:31:14 +00002399 size_t __chash = __constrain_hash(__cn->__hash_, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002400 // find previous node
2401 __node_pointer __pn = __bucket_list_[__chash];
2402 for (; __pn->__next_ != __cn; __pn = __pn->__next_)
2403 ;
2404 // Fix up __bucket_list_
2405 // if __pn is not in same bucket (before begin is not in same bucket) &&
2406 // if __cn->__next_ is not in same bucket (nullptr is not in same bucket)
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00002407 if (__pn == static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()))
2408 || __constrain_hash(__pn->__hash_, __bc) != __chash)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002409 {
Howard Hinnant7a445152012-07-06 17:31:14 +00002410 if (__cn->__next_ == nullptr || __constrain_hash(__cn->__next_->__hash_, __bc) != __chash)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002411 __bucket_list_[__chash] = nullptr;
2412 }
2413 // if __cn->__next_ is not in same bucket (nullptr is in same bucket)
2414 if (__cn->__next_ != nullptr)
2415 {
Howard Hinnant7a445152012-07-06 17:31:14 +00002416 size_t __nhash = __constrain_hash(__cn->__next_->__hash_, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002417 if (__nhash != __chash)
2418 __bucket_list_[__nhash] = __pn;
2419 }
2420 // remove __cn
2421 __pn->__next_ = __cn->__next_;
2422 __cn->__next_ = nullptr;
2423 --size();
Howard Hinnant39213642013-07-23 22:01:58 +00002424#if _LIBCPP_DEBUG_LEVEL >= 2
2425 __c_node* __c = __get_db()->__find_c_and_lock(this);
2426 for (__i_node** __p = __c->end_; __p != __c->beg_; )
2427 {
2428 --__p;
2429 iterator* __i = static_cast<iterator*>((*__p)->__i_);
2430 if (__i->__node_ == __cn)
2431 {
2432 (*__p)->__c_ = nullptr;
2433 if (--__c->end_ != __p)
2434 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
2435 }
2436 }
2437 __get_db()->unlock();
2438#endif
Howard Hinnant99968442011-11-29 18:15:50 +00002439 return __node_holder(__cn, _Dp(__node_alloc(), true));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002440}
2441
2442template <class _Tp, class _Hash, class _Equal, class _Alloc>
2443template <class _Key>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00002444inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002445typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2446__hash_table<_Tp, _Hash, _Equal, _Alloc>::__count_unique(const _Key& __k) const
2447{
2448 return static_cast<size_type>(find(__k) != end());
2449}
2450
2451template <class _Tp, class _Hash, class _Equal, class _Alloc>
2452template <class _Key>
2453typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2454__hash_table<_Tp, _Hash, _Equal, _Alloc>::__count_multi(const _Key& __k) const
2455{
2456 size_type __r = 0;
2457 const_iterator __i = find(__k);
2458 if (__i != end())
2459 {
2460 const_iterator __e = end();
2461 do
2462 {
2463 ++__i;
2464 ++__r;
2465 } while (__i != __e && key_eq()(*__i, __k));
2466 }
2467 return __r;
2468}
2469
2470template <class _Tp, class _Hash, class _Equal, class _Alloc>
2471template <class _Key>
2472pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator,
2473 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator>
2474__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_unique(
2475 const _Key& __k)
2476{
2477 iterator __i = find(__k);
2478 iterator __j = __i;
2479 if (__i != end())
2480 ++__j;
2481 return pair<iterator, iterator>(__i, __j);
2482}
2483
2484template <class _Tp, class _Hash, class _Equal, class _Alloc>
2485template <class _Key>
2486pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator,
2487 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator>
2488__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_unique(
2489 const _Key& __k) const
2490{
2491 const_iterator __i = find(__k);
2492 const_iterator __j = __i;
2493 if (__i != end())
2494 ++__j;
2495 return pair<const_iterator, const_iterator>(__i, __j);
2496}
2497
2498template <class _Tp, class _Hash, class _Equal, class _Alloc>
2499template <class _Key>
2500pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator,
2501 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator>
2502__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_multi(
2503 const _Key& __k)
2504{
2505 iterator __i = find(__k);
2506 iterator __j = __i;
2507 if (__i != end())
2508 {
2509 iterator __e = end();
2510 do
2511 {
2512 ++__j;
2513 } while (__j != __e && key_eq()(*__j, __k));
2514 }
2515 return pair<iterator, iterator>(__i, __j);
2516}
2517
2518template <class _Tp, class _Hash, class _Equal, class _Alloc>
2519template <class _Key>
2520pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator,
2521 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator>
2522__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_multi(
2523 const _Key& __k) const
2524{
2525 const_iterator __i = find(__k);
2526 const_iterator __j = __i;
2527 if (__i != end())
2528 {
2529 const_iterator __e = end();
2530 do
2531 {
2532 ++__j;
2533 } while (__j != __e && key_eq()(*__j, __k));
2534 }
2535 return pair<const_iterator, const_iterator>(__i, __j);
2536}
2537
2538template <class _Tp, class _Hash, class _Equal, class _Alloc>
2539void
2540__hash_table<_Tp, _Hash, _Equal, _Alloc>::swap(__hash_table& __u)
Eric Fiselier692177d2015-07-18 20:40:46 +00002541#if _LIBCPP_STD_VER <= 11
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00002542 _NOEXCEPT_(
Marshall Clow7d914d12015-07-13 20:04:56 +00002543 __is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value
Marshall Clow7d914d12015-07-13 20:04:56 +00002544 && (!allocator_traits<__pointer_allocator>::propagate_on_container_swap::value
2545 || __is_nothrow_swappable<__pointer_allocator>::value)
2546 && (!__node_traits::propagate_on_container_swap::value
2547 || __is_nothrow_swappable<__node_allocator>::value)
Marshall Clow7d914d12015-07-13 20:04:56 +00002548 )
Eric Fiselier692177d2015-07-18 20:40:46 +00002549#else
2550 _NOEXCEPT_(__is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value)
2551#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002552{
2553 {
2554 __node_pointer_pointer __npp = __bucket_list_.release();
2555 __bucket_list_.reset(__u.__bucket_list_.release());
2556 __u.__bucket_list_.reset(__npp);
2557 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002558 _VSTD::swap(__bucket_list_.get_deleter().size(), __u.__bucket_list_.get_deleter().size());
Marshall Clow7d914d12015-07-13 20:04:56 +00002559 __swap_allocator(__bucket_list_.get_deleter().__alloc(),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002560 __u.__bucket_list_.get_deleter().__alloc());
Marshall Clow7d914d12015-07-13 20:04:56 +00002561 __swap_allocator(__node_alloc(), __u.__node_alloc());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002562 _VSTD::swap(__p1_.first().__next_, __u.__p1_.first().__next_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002563 __p2_.swap(__u.__p2_);
2564 __p3_.swap(__u.__p3_);
2565 if (size() > 0)
Howard Hinnant7a445152012-07-06 17:31:14 +00002566 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash_, bucket_count())] =
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00002567 static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002568 if (__u.size() > 0)
Howard Hinnant7a445152012-07-06 17:31:14 +00002569 __u.__bucket_list_[__constrain_hash(__u.__p1_.first().__next_->__hash_, __u.bucket_count())] =
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00002570 static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__u.__p1_.first()));
Howard Hinnant39213642013-07-23 22:01:58 +00002571#if _LIBCPP_DEBUG_LEVEL >= 2
2572 __get_db()->swap(this, &__u);
2573#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002574}
2575
2576template <class _Tp, class _Hash, class _Equal, class _Alloc>
2577typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2578__hash_table<_Tp, _Hash, _Equal, _Alloc>::bucket_size(size_type __n) const
2579{
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00002580 _LIBCPP_ASSERT(__n < bucket_count(),
2581 "unordered container::bucket_size(n) called with n >= bucket_count()");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002582 __node_const_pointer __np = __bucket_list_[__n];
2583 size_type __bc = bucket_count();
2584 size_type __r = 0;
2585 if (__np != nullptr)
2586 {
2587 for (__np = __np->__next_; __np != nullptr &&
Howard Hinnant7a445152012-07-06 17:31:14 +00002588 __constrain_hash(__np->__hash_, __bc) == __n;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002589 __np = __np->__next_, ++__r)
2590 ;
2591 }
2592 return __r;
2593}
2594
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00002595template <class _Tp, class _Hash, class _Equal, class _Alloc>
2596inline _LIBCPP_INLINE_VISIBILITY
2597void
2598swap(__hash_table<_Tp, _Hash, _Equal, _Alloc>& __x,
2599 __hash_table<_Tp, _Hash, _Equal, _Alloc>& __y)
2600 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
2601{
2602 __x.swap(__y);
2603}
2604
Howard Hinnant39213642013-07-23 22:01:58 +00002605#if _LIBCPP_DEBUG_LEVEL >= 2
2606
2607template <class _Tp, class _Hash, class _Equal, class _Alloc>
2608bool
2609__hash_table<_Tp, _Hash, _Equal, _Alloc>::__dereferenceable(const const_iterator* __i) const
2610{
2611 return __i->__node_ != nullptr;
2612}
2613
2614template <class _Tp, class _Hash, class _Equal, class _Alloc>
2615bool
2616__hash_table<_Tp, _Hash, _Equal, _Alloc>::__decrementable(const const_iterator*) const
2617{
2618 return false;
2619}
2620
2621template <class _Tp, class _Hash, class _Equal, class _Alloc>
2622bool
2623__hash_table<_Tp, _Hash, _Equal, _Alloc>::__addable(const const_iterator*, ptrdiff_t) const
2624{
2625 return false;
2626}
2627
2628template <class _Tp, class _Hash, class _Equal, class _Alloc>
2629bool
2630__hash_table<_Tp, _Hash, _Equal, _Alloc>::__subscriptable(const const_iterator*, ptrdiff_t) const
2631{
2632 return false;
2633}
2634
2635#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002636_LIBCPP_END_NAMESPACE_STD
2637
2638#endif // _LIBCPP__HASH_TABLE