blob: 2ea1a305720070553a6f90498ff0231590440fcb [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;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000112
Eric Fiselier774c7c52016-02-10 20:46:23 +0000113template <class _Tp>
Eric Fiselier66e344f2016-02-20 07:59:16 +0000114struct __hash_key_value_types {
Eric Fiselier774c7c52016-02-10 20:46:23 +0000115 static_assert(!is_reference<_Tp>::value && !is_const<_Tp>::value, "");
116 typedef _Tp key_type;
117 typedef _Tp __node_value_type;
118 typedef _Tp __container_value_type;
119 static const bool __is_map = false;
Eric Fiselier2960ae22016-02-11 11:59:44 +0000120
121 _LIBCPP_INLINE_VISIBILITY
122 static key_type const& __get_key(_Tp const& __v) {
123 return __v;
124 }
125 _LIBCPP_INLINE_VISIBILITY
126 static __container_value_type const& __get_value(__node_value_type const& __v) {
127 return __v;
128 }
129 _LIBCPP_INLINE_VISIBILITY
130 static __container_value_type* __get_ptr(__node_value_type& __n) {
131 return _VSTD::addressof(__n);
132 }
133#ifndef _LIBCPP_CXX03_LANG
134 _LIBCPP_INLINE_VISIBILITY
135 static __container_value_type&& __move(__node_value_type& __v) {
136 return _VSTD::move(__v);
137 }
138#endif
Eric Fiselier774c7c52016-02-10 20:46:23 +0000139};
140
141template <class _Key, class _Tp>
Eric Fiselier66e344f2016-02-20 07:59:16 +0000142struct __hash_key_value_types<__hash_value_type<_Key, _Tp> > {
Eric Fiselier774c7c52016-02-10 20:46:23 +0000143 typedef _Key key_type;
144 typedef _Tp mapped_type;
145 typedef __hash_value_type<_Key, _Tp> __node_value_type;
146 typedef pair<const _Key, _Tp> __container_value_type;
Eric Fiselier2960ae22016-02-11 11:59:44 +0000147 typedef pair<_Key, _Tp> __nc_value_type;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000148 typedef __container_value_type __map_value_type;
149 static const bool __is_map = true;
Eric Fiselier2960ae22016-02-11 11:59:44 +0000150
151 _LIBCPP_INLINE_VISIBILITY
152 static key_type const& __get_key(__container_value_type const& __v) {
153 return __v.first;
154 }
155
156 template <class _Up>
157 _LIBCPP_INLINE_VISIBILITY
158 static typename enable_if<__is_same_uncvref<_Up, __node_value_type>::value,
159 __container_value_type const&>::type
160 __get_value(_Up& __t) {
161 return __t.__cc;
162 }
163
164 template <class _Up>
165 _LIBCPP_INLINE_VISIBILITY
166 static typename enable_if<__is_same_uncvref<_Up, __container_value_type>::value,
167 __container_value_type const&>::type
168 __get_value(_Up& __t) {
169 return __t;
170 }
171
172 _LIBCPP_INLINE_VISIBILITY
173 static __container_value_type* __get_ptr(__node_value_type& __n) {
174 return _VSTD::addressof(__n.__cc);
175 }
176#ifndef _LIBCPP_CXX03_LANG
177 _LIBCPP_INLINE_VISIBILITY
178 static __nc_value_type&& __move(__node_value_type& __v) {
179 return _VSTD::move(__v.__nc);
180 }
181#endif
182
Eric Fiselier774c7c52016-02-10 20:46:23 +0000183};
184
Eric Fiselier66e344f2016-02-20 07:59:16 +0000185template <class _Tp, class _AllocPtr, class _KVTypes = __hash_key_value_types<_Tp>,
Eric Fiselier774c7c52016-02-10 20:46:23 +0000186 bool = _KVTypes::__is_map>
Eric Fiselier66e344f2016-02-20 07:59:16 +0000187struct __hash_map_pointer_types {};
Eric Fiselier774c7c52016-02-10 20:46:23 +0000188
189template <class _Tp, class _AllocPtr, class _KVTypes>
Eric Fiselier66e344f2016-02-20 07:59:16 +0000190struct __hash_map_pointer_types<_Tp, _AllocPtr, _KVTypes, true> {
Eric Fiselier774c7c52016-02-10 20:46:23 +0000191 typedef typename _KVTypes::__map_value_type _Mv;
192 typedef typename __rebind_pointer<_AllocPtr, _Mv>::type
193 __map_value_type_pointer;
194 typedef typename __rebind_pointer<_AllocPtr, const _Mv>::type
195 __const_map_value_type_pointer;
196};
197
198template <class _NodePtr, class _NodeT = typename pointer_traits<_NodePtr>::element_type>
199struct __hash_node_types;
200
201template <class _NodePtr, class _Tp, class _VoidPtr>
202struct __hash_node_types<_NodePtr, __hash_node<_Tp, _VoidPtr> >
Eric Fiselier66e344f2016-02-20 07:59:16 +0000203 : public __hash_key_value_types<_Tp>, __hash_map_pointer_types<_Tp, _VoidPtr>
Eric Fiselier774c7c52016-02-10 20:46:23 +0000204
205{
Eric Fiselier66e344f2016-02-20 07:59:16 +0000206 typedef __hash_key_value_types<_Tp> __base;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000207
208public:
209 typedef ptrdiff_t difference_type;
210 typedef size_t size_type;
211
212 typedef typename __rebind_pointer<_NodePtr, void>::type __void_pointer;
213
214 typedef typename pointer_traits<_NodePtr>::element_type __node_type;
215 typedef _NodePtr __node_pointer;
216
217 typedef __hash_node_base<__node_pointer> __node_base_type;
218 typedef typename __rebind_pointer<_NodePtr, __node_base_type>::type
219 __node_base_pointer;
220
221 typedef _Tp __node_value_type;
222 typedef typename __rebind_pointer<_VoidPtr, __node_value_type>::type
223 __node_value_type_pointer;
224 typedef typename __rebind_pointer<_VoidPtr, const __node_value_type>::type
225 __const_node_value_type_pointer;
226private:
227 static_assert(!is_const<__node_type>::value,
228 "_NodePtr should never be a pointer to const");
229 static_assert((is_same<typename pointer_traits<_VoidPtr>::element_type, void>::value),
230 "_VoidPtr does not point to unqualified void type");
231 static_assert((is_same<typename __rebind_pointer<_VoidPtr, __node_type>::type,
232 _NodePtr>::value), "_VoidPtr does not rebind to _NodePtr.");
233};
234
235
236
237template <class _HashIterator>
238struct __hash_node_types_from_iterator;
239template <class _NodePtr>
240struct __hash_node_types_from_iterator<__hash_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
241template <class _NodePtr>
242struct __hash_node_types_from_iterator<__hash_const_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
243template <class _NodePtr>
244struct __hash_node_types_from_iterator<__hash_local_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
245template <class _NodePtr>
246struct __hash_node_types_from_iterator<__hash_const_local_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
247
248
249template <class _NodeValueTp, class _VoidPtr>
250struct __make_hash_node_types {
251 typedef __hash_node<_NodeValueTp, _VoidPtr> _NodeTp;
252 typedef typename __rebind_pointer<_VoidPtr, _NodeTp>::type _NodePtr;
253 typedef __hash_node_types<_NodePtr> type;
254};
255
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000256template <class _NodePtr>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000257class _LIBCPP_TYPE_VIS_ONLY __hash_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000258{
Eric Fiselier774c7c52016-02-10 20:46:23 +0000259 typedef __hash_node_types<_NodePtr> _NodeTypes;
260 typedef _NodePtr __node_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000261
262 __node_pointer __node_;
263
264public:
Eric Fiselier774c7c52016-02-10 20:46:23 +0000265 typedef forward_iterator_tag iterator_category;
266 typedef typename _NodeTypes::__node_value_type value_type;
267 typedef typename _NodeTypes::difference_type difference_type;
268 typedef value_type& reference;
269 typedef typename _NodeTypes::__node_value_type_pointer pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000270
Howard Hinnant39213642013-07-23 22:01:58 +0000271 _LIBCPP_INLINE_VISIBILITY __hash_iterator() _NOEXCEPT
Marshall Clow193ef032013-08-07 21:30:44 +0000272#if _LIBCPP_STD_VER > 11
273 : __node_(nullptr)
274#endif
Howard Hinnant39213642013-07-23 22:01:58 +0000275 {
276#if _LIBCPP_DEBUG_LEVEL >= 2
277 __get_db()->__insert_i(this);
278#endif
279 }
280
281#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000282
Howard Hinnant99acc502010-09-21 17:32:39 +0000283 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000284 __hash_iterator(const __hash_iterator& __i)
285 : __node_(__i.__node_)
286 {
287 __get_db()->__iterator_copy(this, &__i);
288 }
289
Howard Hinnant99acc502010-09-21 17:32:39 +0000290 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000291 ~__hash_iterator()
292 {
293 __get_db()->__erase_i(this);
294 }
295
296 _LIBCPP_INLINE_VISIBILITY
297 __hash_iterator& operator=(const __hash_iterator& __i)
298 {
299 if (this != &__i)
300 {
301 __get_db()->__iterator_copy(this, &__i);
302 __node_ = __i.__node_;
303 }
304 return *this;
305 }
306
307#endif // _LIBCPP_DEBUG_LEVEL >= 2
308
309 _LIBCPP_INLINE_VISIBILITY
310 reference operator*() const
311 {
312#if _LIBCPP_DEBUG_LEVEL >= 2
313 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
314 "Attempted to dereference a non-dereferenceable unordered container iterator");
315#endif
316 return __node_->__value_;
317 }
318 _LIBCPP_INLINE_VISIBILITY
319 pointer operator->() const
320 {
321#if _LIBCPP_DEBUG_LEVEL >= 2
322 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
323 "Attempted to dereference a non-dereferenceable unordered container iterator");
324#endif
325 return pointer_traits<pointer>::pointer_to(__node_->__value_);
326 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000327
Howard Hinnant99acc502010-09-21 17:32:39 +0000328 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000329 __hash_iterator& operator++()
330 {
Howard Hinnant39213642013-07-23 22:01:58 +0000331#if _LIBCPP_DEBUG_LEVEL >= 2
332 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
333 "Attempted to increment non-incrementable unordered container iterator");
334#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000335 __node_ = __node_->__next_;
336 return *this;
337 }
338
Howard Hinnant99acc502010-09-21 17:32:39 +0000339 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000340 __hash_iterator operator++(int)
341 {
342 __hash_iterator __t(*this);
343 ++(*this);
344 return __t;
345 }
346
Howard Hinnant99acc502010-09-21 17:32:39 +0000347 friend _LIBCPP_INLINE_VISIBILITY
348 bool operator==(const __hash_iterator& __x, const __hash_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58 +0000349 {
Howard Hinnant39213642013-07-23 22:01:58 +0000350 return __x.__node_ == __y.__node_;
351 }
Howard Hinnant99acc502010-09-21 17:32:39 +0000352 friend _LIBCPP_INLINE_VISIBILITY
353 bool operator!=(const __hash_iterator& __x, const __hash_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58 +0000354 {return !(__x == __y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000355
356private:
Howard Hinnant39213642013-07-23 22:01:58 +0000357#if _LIBCPP_DEBUG_LEVEL >= 2
358 _LIBCPP_INLINE_VISIBILITY
359 __hash_iterator(__node_pointer __node, const void* __c) _NOEXCEPT
360 : __node_(__node)
361 {
362 __get_db()->__insert_ic(this, __c);
363 }
364#else
Howard Hinnant99acc502010-09-21 17:32:39 +0000365 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000366 __hash_iterator(__node_pointer __node) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000367 : __node_(__node)
368 {}
Howard Hinnant39213642013-07-23 22:01:58 +0000369#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000370
371 template <class, class, class, class> friend class __hash_table;
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000372 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_const_iterator;
373 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_map_iterator;
374 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_map;
375 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_multimap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000376};
377
Eric Fiselier774c7c52016-02-10 20:46:23 +0000378template <class _NodePtr>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000379class _LIBCPP_TYPE_VIS_ONLY __hash_const_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000380{
Eric Fiselier774c7c52016-02-10 20:46:23 +0000381 static_assert(!is_const<typename pointer_traits<_NodePtr>::element_type>::value, "");
382 typedef __hash_node_types<_NodePtr> _NodeTypes;
383 typedef _NodePtr __node_pointer;
Eric Fiselier0493d022016-02-18 00:20:34 +0000384
Eric Fiselier774c7c52016-02-10 20:46:23 +0000385 __node_pointer __node_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000386
387public:
Eric Fiselier0493d022016-02-18 00:20:34 +0000388 typedef __hash_iterator<_NodePtr> __non_const_iterator;
389
Eric Fiselier774c7c52016-02-10 20:46:23 +0000390 typedef forward_iterator_tag iterator_category;
391 typedef typename _NodeTypes::__node_value_type value_type;
392 typedef typename _NodeTypes::difference_type difference_type;
393 typedef const value_type& reference;
394 typedef typename _NodeTypes::__const_node_value_type_pointer pointer;
395
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000396
Howard Hinnant39213642013-07-23 22:01:58 +0000397 _LIBCPP_INLINE_VISIBILITY __hash_const_iterator() _NOEXCEPT
Marshall Clow193ef032013-08-07 21:30:44 +0000398#if _LIBCPP_STD_VER > 11
399 : __node_(nullptr)
400#endif
Howard Hinnant39213642013-07-23 22:01:58 +0000401 {
402#if _LIBCPP_DEBUG_LEVEL >= 2
403 __get_db()->__insert_i(this);
404#endif
405 }
Howard Hinnant99acc502010-09-21 17:32:39 +0000406 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000407 __hash_const_iterator(const __non_const_iterator& __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000408 : __node_(__x.__node_)
Howard Hinnant39213642013-07-23 22:01:58 +0000409 {
410#if _LIBCPP_DEBUG_LEVEL >= 2
411 __get_db()->__iterator_copy(this, &__x);
412#endif
413 }
414
415#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000416
Howard Hinnant99acc502010-09-21 17:32:39 +0000417 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000418 __hash_const_iterator(const __hash_const_iterator& __i)
419 : __node_(__i.__node_)
420 {
421 __get_db()->__iterator_copy(this, &__i);
422 }
423
Howard Hinnant99acc502010-09-21 17:32:39 +0000424 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000425 ~__hash_const_iterator()
426 {
427 __get_db()->__erase_i(this);
428 }
429
430 _LIBCPP_INLINE_VISIBILITY
431 __hash_const_iterator& operator=(const __hash_const_iterator& __i)
432 {
433 if (this != &__i)
434 {
435 __get_db()->__iterator_copy(this, &__i);
436 __node_ = __i.__node_;
437 }
438 return *this;
439 }
440
441#endif // _LIBCPP_DEBUG_LEVEL >= 2
442
443 _LIBCPP_INLINE_VISIBILITY
444 reference operator*() const
445 {
446#if _LIBCPP_DEBUG_LEVEL >= 2
447 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
448 "Attempted to dereference a non-dereferenceable unordered container const_iterator");
449#endif
450 return __node_->__value_;
451 }
452 _LIBCPP_INLINE_VISIBILITY
453 pointer operator->() const
454 {
455#if _LIBCPP_DEBUG_LEVEL >= 2
456 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
457 "Attempted to dereference a non-dereferenceable unordered container const_iterator");
458#endif
459 return pointer_traits<pointer>::pointer_to(__node_->__value_);
460 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000461
Howard Hinnant99acc502010-09-21 17:32:39 +0000462 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000463 __hash_const_iterator& operator++()
464 {
Howard Hinnant39213642013-07-23 22:01:58 +0000465#if _LIBCPP_DEBUG_LEVEL >= 2
466 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
467 "Attempted to increment non-incrementable unordered container const_iterator");
468#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000469 __node_ = __node_->__next_;
470 return *this;
471 }
472
Howard Hinnant99acc502010-09-21 17:32:39 +0000473 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000474 __hash_const_iterator operator++(int)
475 {
476 __hash_const_iterator __t(*this);
477 ++(*this);
478 return __t;
479 }
480
Howard Hinnant99acc502010-09-21 17:32:39 +0000481 friend _LIBCPP_INLINE_VISIBILITY
482 bool operator==(const __hash_const_iterator& __x, const __hash_const_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58 +0000483 {
Howard Hinnant39213642013-07-23 22:01:58 +0000484 return __x.__node_ == __y.__node_;
485 }
Howard Hinnant99acc502010-09-21 17:32:39 +0000486 friend _LIBCPP_INLINE_VISIBILITY
487 bool operator!=(const __hash_const_iterator& __x, const __hash_const_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58 +0000488 {return !(__x == __y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000489
490private:
Howard Hinnant39213642013-07-23 22:01:58 +0000491#if _LIBCPP_DEBUG_LEVEL >= 2
492 _LIBCPP_INLINE_VISIBILITY
493 __hash_const_iterator(__node_pointer __node, const void* __c) _NOEXCEPT
494 : __node_(__node)
495 {
496 __get_db()->__insert_ic(this, __c);
497 }
498#else
Howard Hinnant99acc502010-09-21 17:32:39 +0000499 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000500 __hash_const_iterator(__node_pointer __node) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000501 : __node_(__node)
502 {}
Howard Hinnant39213642013-07-23 22:01:58 +0000503#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000504
505 template <class, class, class, class> friend class __hash_table;
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000506 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_map_const_iterator;
507 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_map;
508 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_multimap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000509};
510
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000511template <class _NodePtr>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000512class _LIBCPP_TYPE_VIS_ONLY __hash_local_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000513{
Eric Fiselier774c7c52016-02-10 20:46:23 +0000514 typedef __hash_node_types<_NodePtr> _NodeTypes;
515 typedef _NodePtr __node_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000516
517 __node_pointer __node_;
518 size_t __bucket_;
519 size_t __bucket_count_;
520
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000521public:
522 typedef forward_iterator_tag iterator_category;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000523 typedef typename _NodeTypes::__node_value_type value_type;
524 typedef typename _NodeTypes::difference_type difference_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000525 typedef value_type& reference;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000526 typedef typename _NodeTypes::__node_value_type_pointer pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000527
Howard Hinnant39213642013-07-23 22:01:58 +0000528 _LIBCPP_INLINE_VISIBILITY __hash_local_iterator() _NOEXCEPT
529 {
530#if _LIBCPP_DEBUG_LEVEL >= 2
531 __get_db()->__insert_i(this);
532#endif
533 }
534
535#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000536
Howard Hinnant99acc502010-09-21 17:32:39 +0000537 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000538 __hash_local_iterator(const __hash_local_iterator& __i)
539 : __node_(__i.__node_),
540 __bucket_(__i.__bucket_),
541 __bucket_count_(__i.__bucket_count_)
542 {
543 __get_db()->__iterator_copy(this, &__i);
544 }
545
Howard Hinnant99acc502010-09-21 17:32:39 +0000546 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000547 ~__hash_local_iterator()
548 {
549 __get_db()->__erase_i(this);
550 }
551
552 _LIBCPP_INLINE_VISIBILITY
553 __hash_local_iterator& operator=(const __hash_local_iterator& __i)
554 {
555 if (this != &__i)
556 {
557 __get_db()->__iterator_copy(this, &__i);
558 __node_ = __i.__node_;
559 __bucket_ = __i.__bucket_;
560 __bucket_count_ = __i.__bucket_count_;
561 }
562 return *this;
563 }
564
565#endif // _LIBCPP_DEBUG_LEVEL >= 2
566
567 _LIBCPP_INLINE_VISIBILITY
568 reference operator*() const
569 {
570#if _LIBCPP_DEBUG_LEVEL >= 2
571 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
572 "Attempted to dereference a non-dereferenceable unordered container local_iterator");
573#endif
574 return __node_->__value_;
575 }
576 _LIBCPP_INLINE_VISIBILITY
577 pointer operator->() const
578 {
579#if _LIBCPP_DEBUG_LEVEL >= 2
580 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
581 "Attempted to dereference a non-dereferenceable unordered container local_iterator");
582#endif
583 return pointer_traits<pointer>::pointer_to(__node_->__value_);
584 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000585
Howard Hinnant99acc502010-09-21 17:32:39 +0000586 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000587 __hash_local_iterator& operator++()
588 {
Howard Hinnant39213642013-07-23 22:01:58 +0000589#if _LIBCPP_DEBUG_LEVEL >= 2
590 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
591 "Attempted to increment non-incrementable unordered container local_iterator");
592#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000593 __node_ = __node_->__next_;
Howard Hinnant7a445152012-07-06 17:31:14 +0000594 if (__node_ != nullptr && __constrain_hash(__node_->__hash_, __bucket_count_) != __bucket_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000595 __node_ = nullptr;
596 return *this;
597 }
598
Howard Hinnant99acc502010-09-21 17:32:39 +0000599 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000600 __hash_local_iterator operator++(int)
601 {
602 __hash_local_iterator __t(*this);
603 ++(*this);
604 return __t;
605 }
606
Howard Hinnant99acc502010-09-21 17:32:39 +0000607 friend _LIBCPP_INLINE_VISIBILITY
608 bool operator==(const __hash_local_iterator& __x, const __hash_local_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58 +0000609 {
Howard Hinnant39213642013-07-23 22:01:58 +0000610 return __x.__node_ == __y.__node_;
611 }
Howard Hinnant99acc502010-09-21 17:32:39 +0000612 friend _LIBCPP_INLINE_VISIBILITY
613 bool operator!=(const __hash_local_iterator& __x, const __hash_local_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58 +0000614 {return !(__x == __y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000615
616private:
Howard Hinnant39213642013-07-23 22:01:58 +0000617#if _LIBCPP_DEBUG_LEVEL >= 2
618 _LIBCPP_INLINE_VISIBILITY
619 __hash_local_iterator(__node_pointer __node, size_t __bucket,
620 size_t __bucket_count, const void* __c) _NOEXCEPT
621 : __node_(__node),
622 __bucket_(__bucket),
623 __bucket_count_(__bucket_count)
624 {
625 __get_db()->__insert_ic(this, __c);
626 if (__node_ != nullptr)
627 __node_ = __node_->__next_;
628 }
629#else
Howard Hinnant99acc502010-09-21 17:32:39 +0000630 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000631 __hash_local_iterator(__node_pointer __node, size_t __bucket,
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000632 size_t __bucket_count) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000633 : __node_(__node),
634 __bucket_(__bucket),
635 __bucket_count_(__bucket_count)
636 {
637 if (__node_ != nullptr)
638 __node_ = __node_->__next_;
639 }
Howard Hinnant39213642013-07-23 22:01:58 +0000640#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000641 template <class, class, class, class> friend class __hash_table;
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000642 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_const_local_iterator;
643 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_map_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000644};
645
646template <class _ConstNodePtr>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000647class _LIBCPP_TYPE_VIS_ONLY __hash_const_local_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000648{
Eric Fiselier774c7c52016-02-10 20:46:23 +0000649 typedef __hash_node_types<_ConstNodePtr> _NodeTypes;
650 typedef _ConstNodePtr __node_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000651
652 __node_pointer __node_;
653 size_t __bucket_;
654 size_t __bucket_count_;
655
656 typedef pointer_traits<__node_pointer> __pointer_traits;
657 typedef typename __pointer_traits::element_type __node;
658 typedef typename remove_const<__node>::type __non_const_node;
Eric Fiselier5cf84e02015-12-30 21:52:00 +0000659 typedef typename __rebind_pointer<__node_pointer, __non_const_node>::type
660 __non_const_node_pointer;
Eric Fiselier0493d022016-02-18 00:20:34 +0000661public:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000662 typedef __hash_local_iterator<__non_const_node_pointer>
663 __non_const_iterator;
Eric Fiselier0493d022016-02-18 00:20:34 +0000664
Eric Fiselier774c7c52016-02-10 20:46:23 +0000665 typedef forward_iterator_tag iterator_category;
666 typedef typename _NodeTypes::__node_value_type value_type;
667 typedef typename _NodeTypes::difference_type difference_type;
668 typedef const value_type& reference;
669 typedef typename _NodeTypes::__const_node_value_type_pointer pointer;
Eric Fiselier5cf84e02015-12-30 21:52:00 +0000670
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000671
Howard Hinnant39213642013-07-23 22:01:58 +0000672 _LIBCPP_INLINE_VISIBILITY __hash_const_local_iterator() _NOEXCEPT
673 {
674#if _LIBCPP_DEBUG_LEVEL >= 2
675 __get_db()->__insert_i(this);
676#endif
677 }
678
Howard Hinnant99acc502010-09-21 17:32:39 +0000679 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000680 __hash_const_local_iterator(const __non_const_iterator& __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000681 : __node_(__x.__node_),
682 __bucket_(__x.__bucket_),
683 __bucket_count_(__x.__bucket_count_)
Howard Hinnant39213642013-07-23 22:01:58 +0000684 {
685#if _LIBCPP_DEBUG_LEVEL >= 2
686 __get_db()->__iterator_copy(this, &__x);
687#endif
688 }
689
690#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000691
Howard Hinnant99acc502010-09-21 17:32:39 +0000692 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000693 __hash_const_local_iterator(const __hash_const_local_iterator& __i)
694 : __node_(__i.__node_),
695 __bucket_(__i.__bucket_),
696 __bucket_count_(__i.__bucket_count_)
697 {
698 __get_db()->__iterator_copy(this, &__i);
699 }
700
Howard Hinnant99acc502010-09-21 17:32:39 +0000701 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000702 ~__hash_const_local_iterator()
703 {
704 __get_db()->__erase_i(this);
705 }
706
707 _LIBCPP_INLINE_VISIBILITY
708 __hash_const_local_iterator& operator=(const __hash_const_local_iterator& __i)
709 {
710 if (this != &__i)
711 {
712 __get_db()->__iterator_copy(this, &__i);
713 __node_ = __i.__node_;
714 __bucket_ = __i.__bucket_;
715 __bucket_count_ = __i.__bucket_count_;
716 }
717 return *this;
718 }
719
720#endif // _LIBCPP_DEBUG_LEVEL >= 2
721
722 _LIBCPP_INLINE_VISIBILITY
723 reference operator*() const
724 {
725#if _LIBCPP_DEBUG_LEVEL >= 2
726 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
727 "Attempted to dereference a non-dereferenceable unordered container const_local_iterator");
728#endif
729 return __node_->__value_;
730 }
731 _LIBCPP_INLINE_VISIBILITY
732 pointer operator->() const
733 {
734#if _LIBCPP_DEBUG_LEVEL >= 2
735 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
736 "Attempted to dereference a non-dereferenceable unordered container const_local_iterator");
737#endif
738 return pointer_traits<pointer>::pointer_to(__node_->__value_);
739 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000740
Howard Hinnant99acc502010-09-21 17:32:39 +0000741 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000742 __hash_const_local_iterator& operator++()
743 {
Howard Hinnant39213642013-07-23 22:01:58 +0000744#if _LIBCPP_DEBUG_LEVEL >= 2
745 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
746 "Attempted to increment non-incrementable unordered container const_local_iterator");
747#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000748 __node_ = __node_->__next_;
Howard Hinnant7a445152012-07-06 17:31:14 +0000749 if (__node_ != nullptr && __constrain_hash(__node_->__hash_, __bucket_count_) != __bucket_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000750 __node_ = nullptr;
751 return *this;
752 }
753
Howard Hinnant99acc502010-09-21 17:32:39 +0000754 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000755 __hash_const_local_iterator operator++(int)
756 {
757 __hash_const_local_iterator __t(*this);
758 ++(*this);
759 return __t;
760 }
761
Howard Hinnant99acc502010-09-21 17:32:39 +0000762 friend _LIBCPP_INLINE_VISIBILITY
763 bool operator==(const __hash_const_local_iterator& __x, const __hash_const_local_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58 +0000764 {
Howard Hinnant39213642013-07-23 22:01:58 +0000765 return __x.__node_ == __y.__node_;
766 }
Howard Hinnant99acc502010-09-21 17:32:39 +0000767 friend _LIBCPP_INLINE_VISIBILITY
768 bool operator!=(const __hash_const_local_iterator& __x, const __hash_const_local_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58 +0000769 {return !(__x == __y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000770
771private:
Howard Hinnant39213642013-07-23 22:01:58 +0000772#if _LIBCPP_DEBUG_LEVEL >= 2
773 _LIBCPP_INLINE_VISIBILITY
774 __hash_const_local_iterator(__node_pointer __node, size_t __bucket,
775 size_t __bucket_count, const void* __c) _NOEXCEPT
776 : __node_(__node),
777 __bucket_(__bucket),
778 __bucket_count_(__bucket_count)
779 {
780 __get_db()->__insert_ic(this, __c);
781 if (__node_ != nullptr)
782 __node_ = __node_->__next_;
783 }
784#else
Howard Hinnant99acc502010-09-21 17:32:39 +0000785 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000786 __hash_const_local_iterator(__node_pointer __node, size_t __bucket,
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000787 size_t __bucket_count) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000788 : __node_(__node),
789 __bucket_(__bucket),
790 __bucket_count_(__bucket_count)
791 {
792 if (__node_ != nullptr)
793 __node_ = __node_->__next_;
794 }
Howard Hinnant39213642013-07-23 22:01:58 +0000795#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000796 template <class, class, class, class> friend class __hash_table;
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000797 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_map_const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000798};
799
800template <class _Alloc>
801class __bucket_list_deallocator
802{
803 typedef _Alloc allocator_type;
804 typedef allocator_traits<allocator_type> __alloc_traits;
805 typedef typename __alloc_traits::size_type size_type;
806
807 __compressed_pair<size_type, allocator_type> __data_;
808public:
809 typedef typename __alloc_traits::pointer pointer;
810
Howard Hinnant99acc502010-09-21 17:32:39 +0000811 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000812 __bucket_list_deallocator()
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000813 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000814 : __data_(0) {}
Howard Hinnant99acc502010-09-21 17:32:39 +0000815
816 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000817 __bucket_list_deallocator(const allocator_type& __a, size_type __size)
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000818 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000819 : __data_(__size, __a) {}
820
Howard Hinnant73d21a42010-09-04 23:28:19 +0000821#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000822
Howard Hinnant99acc502010-09-21 17:32:39 +0000823 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000824 __bucket_list_deallocator(__bucket_list_deallocator&& __x)
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000825 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000826 : __data_(_VSTD::move(__x.__data_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000827 {
828 __x.size() = 0;
829 }
830
Howard Hinnant73d21a42010-09-04 23:28:19 +0000831#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000832
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000833 _LIBCPP_INLINE_VISIBILITY
834 size_type& size() _NOEXCEPT {return __data_.first();}
835 _LIBCPP_INLINE_VISIBILITY
836 size_type size() const _NOEXCEPT {return __data_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000837
Howard Hinnant99acc502010-09-21 17:32:39 +0000838 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000839 allocator_type& __alloc() _NOEXCEPT {return __data_.second();}
840 _LIBCPP_INLINE_VISIBILITY
841 const allocator_type& __alloc() const _NOEXCEPT {return __data_.second();}
842
843 _LIBCPP_INLINE_VISIBILITY
844 void operator()(pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000845 {
846 __alloc_traits::deallocate(__alloc(), __p, size());
847 }
848};
849
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000850template <class _Alloc> class __hash_map_node_destructor;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000851
852template <class _Alloc>
853class __hash_node_destructor
854{
855 typedef _Alloc allocator_type;
856 typedef allocator_traits<allocator_type> __alloc_traits;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000857
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000858public:
859 typedef typename __alloc_traits::pointer pointer;
860private:
Eric Fiselier2960ae22016-02-11 11:59:44 +0000861 typedef __hash_node_types<pointer> _NodeTypes;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000862
863 allocator_type& __na_;
864
865 __hash_node_destructor& operator=(const __hash_node_destructor&);
866
867public:
868 bool __value_constructed;
869
Howard Hinnant99acc502010-09-21 17:32:39 +0000870 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant199d0ae2011-07-31 17:04:30 +0000871 explicit __hash_node_destructor(allocator_type& __na,
872 bool __constructed = false) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000873 : __na_(__na),
Howard Hinnant199d0ae2011-07-31 17:04:30 +0000874 __value_constructed(__constructed)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000875 {}
876
Howard Hinnant99acc502010-09-21 17:32:39 +0000877 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000878 void operator()(pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000879 {
880 if (__value_constructed)
Eric Fiselier2960ae22016-02-11 11:59:44 +0000881 __alloc_traits::destroy(__na_, _NodeTypes::__get_ptr(__p->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000882 if (__p)
883 __alloc_traits::deallocate(__na_, __p, 1);
884 }
885
886 template <class> friend class __hash_map_node_destructor;
887};
888
889template <class _Tp, class _Hash, class _Equal, class _Alloc>
890class __hash_table
891{
892public:
893 typedef _Tp value_type;
894 typedef _Hash hasher;
895 typedef _Equal key_equal;
896 typedef _Alloc allocator_type;
897
898private:
899 typedef allocator_traits<allocator_type> __alloc_traits;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000900 typedef typename
901 __make_hash_node_types<value_type, typename __alloc_traits::void_pointer>::type
902 _NodeTypes;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000903public:
Eric Fiselier2960ae22016-02-11 11:59:44 +0000904
905 typedef typename _NodeTypes::__node_value_type __node_value_type;
906 typedef typename _NodeTypes::__container_value_type __container_value_type;
Duncan P. N. Exon Smith798ec842016-03-17 20:45:20 +0000907 typedef typename _NodeTypes::key_type key_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000908 typedef value_type& reference;
909 typedef const value_type& const_reference;
910 typedef typename __alloc_traits::pointer pointer;
911 typedef typename __alloc_traits::const_pointer const_pointer;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000912#ifndef _LIBCPP_ABI_FIX_UNORDERED_CONTAINER_SIZE_TYPE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000913 typedef typename __alloc_traits::size_type size_type;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000914#else
915 typedef typename _NodeTypes::size_type size_type;
916#endif
917 typedef typename _NodeTypes::difference_type difference_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000918public:
919 // Create __node
Eric Fiselier774c7c52016-02-10 20:46:23 +0000920
921 typedef typename _NodeTypes::__node_type __node;
Marshall Clow66302c62015-04-07 05:21:38 +0000922 typedef typename __rebind_alloc_helper<__alloc_traits, __node>::type __node_allocator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000923 typedef allocator_traits<__node_allocator> __node_traits;
Eric Fiselier9e9f42e2016-02-11 15:22:37 +0000924 typedef typename _NodeTypes::__void_pointer __void_pointer;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000925 typedef typename _NodeTypes::__node_pointer __node_pointer;
926 typedef typename _NodeTypes::__node_pointer __node_const_pointer;
927 typedef typename _NodeTypes::__node_base_type __first_node;
928 typedef typename _NodeTypes::__node_base_pointer __node_base_pointer;
929
930private:
931 // check for sane allocator pointer rebinding semantics. Rebinding the
932 // allocator for a new pointer type should be exactly the same as rebinding
933 // the pointer using 'pointer_traits'.
934 static_assert((is_same<__node_pointer, typename __node_traits::pointer>::value),
935 "Allocator does not rebind pointers in a sane manner.");
936 typedef typename __rebind_alloc_helper<__node_traits, __first_node>::type
937 __node_base_allocator;
938 typedef allocator_traits<__node_base_allocator> __node_base_traits;
939 static_assert((is_same<__node_base_pointer, typename __node_base_traits::pointer>::value),
940 "Allocator does not rebind pointers in a sane manner.");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000941
942private:
943
Marshall Clow66302c62015-04-07 05:21:38 +0000944 typedef typename __rebind_alloc_helper<__node_traits, __node_pointer>::type __pointer_allocator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000945 typedef __bucket_list_deallocator<__pointer_allocator> __bucket_list_deleter;
946 typedef unique_ptr<__node_pointer[], __bucket_list_deleter> __bucket_list;
947 typedef allocator_traits<__pointer_allocator> __pointer_alloc_traits;
948 typedef typename __bucket_list_deleter::pointer __node_pointer_pointer;
949
950 // --- Member data begin ---
Eric Fiselier774c7c52016-02-10 20:46:23 +0000951 __bucket_list __bucket_list_;
952 __compressed_pair<__first_node, __node_allocator> __p1_;
953 __compressed_pair<size_type, hasher> __p2_;
954 __compressed_pair<float, key_equal> __p3_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000955 // --- Member data end ---
956
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000957 _LIBCPP_INLINE_VISIBILITY
958 size_type& size() _NOEXCEPT {return __p2_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000959public:
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000960 _LIBCPP_INLINE_VISIBILITY
961 size_type size() const _NOEXCEPT {return __p2_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000962
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000963 _LIBCPP_INLINE_VISIBILITY
964 hasher& hash_function() _NOEXCEPT {return __p2_.second();}
965 _LIBCPP_INLINE_VISIBILITY
966 const hasher& hash_function() const _NOEXCEPT {return __p2_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000967
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000968 _LIBCPP_INLINE_VISIBILITY
969 float& max_load_factor() _NOEXCEPT {return __p3_.first();}
970 _LIBCPP_INLINE_VISIBILITY
971 float max_load_factor() const _NOEXCEPT {return __p3_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000972
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000973 _LIBCPP_INLINE_VISIBILITY
974 key_equal& key_eq() _NOEXCEPT {return __p3_.second();}
975 _LIBCPP_INLINE_VISIBILITY
976 const key_equal& key_eq() const _NOEXCEPT {return __p3_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000977
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000978 _LIBCPP_INLINE_VISIBILITY
979 __node_allocator& __node_alloc() _NOEXCEPT {return __p1_.second();}
980 _LIBCPP_INLINE_VISIBILITY
981 const __node_allocator& __node_alloc() const _NOEXCEPT
982 {return __p1_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000983
984public:
985 typedef __hash_iterator<__node_pointer> iterator;
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +0000986 typedef __hash_const_iterator<__node_pointer> const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000987 typedef __hash_local_iterator<__node_pointer> local_iterator;
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +0000988 typedef __hash_const_local_iterator<__node_pointer> const_local_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000989
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000990 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000991 __hash_table()
992 _NOEXCEPT_(
993 is_nothrow_default_constructible<__bucket_list>::value &&
994 is_nothrow_default_constructible<__first_node>::value &&
995 is_nothrow_default_constructible<__node_allocator>::value &&
996 is_nothrow_default_constructible<hasher>::value &&
997 is_nothrow_default_constructible<key_equal>::value);
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000998 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000999 __hash_table(const hasher& __hf, const key_equal& __eql);
1000 __hash_table(const hasher& __hf, const key_equal& __eql,
1001 const allocator_type& __a);
1002 explicit __hash_table(const allocator_type& __a);
1003 __hash_table(const __hash_table& __u);
1004 __hash_table(const __hash_table& __u, const allocator_type& __a);
Eric Fiselier2960ae22016-02-11 11:59:44 +00001005#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001006 __hash_table(__hash_table&& __u)
1007 _NOEXCEPT_(
1008 is_nothrow_move_constructible<__bucket_list>::value &&
1009 is_nothrow_move_constructible<__first_node>::value &&
1010 is_nothrow_move_constructible<__node_allocator>::value &&
1011 is_nothrow_move_constructible<hasher>::value &&
1012 is_nothrow_move_constructible<key_equal>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001013 __hash_table(__hash_table&& __u, const allocator_type& __a);
Eric Fiselier2960ae22016-02-11 11:59:44 +00001014#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001015 ~__hash_table();
1016
1017 __hash_table& operator=(const __hash_table& __u);
Eric Fiselier2960ae22016-02-11 11:59:44 +00001018#ifndef _LIBCPP_CXX03_LANG
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001019 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001020 __hash_table& operator=(__hash_table&& __u)
1021 _NOEXCEPT_(
1022 __node_traits::propagate_on_container_move_assignment::value &&
1023 is_nothrow_move_assignable<__node_allocator>::value &&
1024 is_nothrow_move_assignable<hasher>::value &&
1025 is_nothrow_move_assignable<key_equal>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001026#endif
1027 template <class _InputIterator>
1028 void __assign_unique(_InputIterator __first, _InputIterator __last);
1029 template <class _InputIterator>
1030 void __assign_multi(_InputIterator __first, _InputIterator __last);
1031
Howard Hinnant99acc502010-09-21 17:32:39 +00001032 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001033 size_type max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001034 {
1035 return allocator_traits<__pointer_allocator>::max_size(
1036 __bucket_list_.get_deleter().__alloc());
1037 }
1038
1039 pair<iterator, bool> __node_insert_unique(__node_pointer __nd);
1040 iterator __node_insert_multi(__node_pointer __nd);
1041 iterator __node_insert_multi(const_iterator __p,
1042 __node_pointer __nd);
1043
Eric Fiselier2960ae22016-02-11 11:59:44 +00001044#ifndef _LIBCPP_CXX03_LANG
1045 template <class _Key, class ..._Args>
Duncan P. N. Exon Smith798ec842016-03-17 20:45:20 +00001046 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2960ae22016-02-11 11:59:44 +00001047 pair<iterator, bool> __emplace_unique_key_args(_Key const& __k, _Args&&... __args);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001048
Eric Fiselier2960ae22016-02-11 11:59:44 +00001049 template <class... _Args>
Duncan P. N. Exon Smith798ec842016-03-17 20:45:20 +00001050 _LIBCPP_INLINE_VISIBILITY
1051 pair<iterator, bool> __emplace_unique_impl(_Args&&... __args);
1052
1053 template <class _Pp>
1054 _LIBCPP_INLINE_VISIBILITY
1055 pair<iterator, bool> __emplace_unique(_Pp&& __x) {
1056 return __emplace_unique_extract_key(_VSTD::forward<_Pp>(__x),
1057 __can_extract_key<_Pp, key_type>());
1058 }
Eric Fiselier2960ae22016-02-11 11:59:44 +00001059 template <class... _Args>
Duncan P. N. Exon Smith798ec842016-03-17 20:45:20 +00001060 _LIBCPP_INLINE_VISIBILITY
1061 pair<iterator, bool> __emplace_unique(_Args&&... __args) {
1062 return __emplace_unique_impl(_VSTD::forward<_Args>(__args)...);
1063 }
1064
1065 template <class _Pp>
1066 _LIBCPP_INLINE_VISIBILITY
1067 pair<iterator, bool>
1068 __emplace_unique_extract_key(_Pp&& __x, __extract_key_fail_tag) {
1069 return __emplace_unique_impl(_VSTD::forward<_Pp>(__x));
1070 }
1071 template <class _Pp>
1072 _LIBCPP_INLINE_VISIBILITY
1073 pair<iterator, bool>
1074 __emplace_unique_extract_key(_Pp&& __x, __extract_key_self_tag) {
1075 return __emplace_unique_key_args(__x, _VSTD::forward<_Pp>(__x));
1076 }
1077 template <class _Pp>
1078 _LIBCPP_INLINE_VISIBILITY
1079 pair<iterator, bool>
1080 __emplace_unique_extract_key(_Pp&& __x, __extract_key_first_tag) {
1081 return __emplace_unique_key_args(__x.first, _VSTD::forward<_Pp>(__x));
1082 }
1083
1084 template <class... _Args>
1085 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2960ae22016-02-11 11:59:44 +00001086 iterator __emplace_multi(_Args&&... __args);
1087 template <class... _Args>
Duncan P. N. Exon Smith798ec842016-03-17 20:45:20 +00001088 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2960ae22016-02-11 11:59:44 +00001089 iterator __emplace_hint_multi(const_iterator __p, _Args&&... __args);
1090
1091
Eric Fiselierfdae69a2015-06-13 07:18:32 +00001092 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2960ae22016-02-11 11:59:44 +00001093 pair<iterator, bool>
1094 __insert_unique(__container_value_type&& __x) {
1095 return __emplace_unique_key_args(_NodeTypes::__get_key(__x), _VSTD::move(__x));
1096 }
1097
1098 template <class _Pp, class = typename enable_if<
1099 !__is_same_uncvref<_Pp, __container_value_type>::value
1100 >::type>
Eric Fiselierfdae69a2015-06-13 07:18:32 +00001101 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2960ae22016-02-11 11:59:44 +00001102 pair<iterator, bool> __insert_unique(_Pp&& __x) {
1103 return __emplace_unique(_VSTD::forward<_Pp>(__x));
1104 }
1105
1106 template <class _Pp>
1107 _LIBCPP_INLINE_VISIBILITY
1108 iterator __insert_multi(_Pp&& __x) {
1109 return __emplace_multi(_VSTD::forward<_Pp>(__x));
1110 }
1111
1112 template <class _Pp>
1113 _LIBCPP_INLINE_VISIBILITY
1114 iterator __insert_multi(const_iterator __p, _Pp&& __x) {
1115 return __emplace_hint_multi(__p, _VSTD::forward<_Pp>(__x));
1116 }
1117
1118#else // !defined(_LIBCPP_CXX03_LANG)
1119 template <class _Key, class _Args>
1120 pair<iterator, bool> __emplace_unique_key_args(_Key const&, _Args& __args);
1121
1122 iterator __insert_multi(const __container_value_type& __x);
1123 iterator __insert_multi(const_iterator __p, const __container_value_type& __x);
Eric Fiselierfdae69a2015-06-13 07:18:32 +00001124#endif
1125
Eric Fiselier2960ae22016-02-11 11:59:44 +00001126 _LIBCPP_INLINE_VISIBILITY
1127 pair<iterator, bool> __insert_unique(const __container_value_type& __x) {
1128 return __emplace_unique_key_args(_NodeTypes::__get_key(__x), __x);
1129 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001130
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001131 void clear() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001132 void rehash(size_type __n);
Howard Hinnant99acc502010-09-21 17:32:39 +00001133 _LIBCPP_INLINE_VISIBILITY void reserve(size_type __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001134 {rehash(static_cast<size_type>(ceil(__n / max_load_factor())));}
Howard Hinnant99acc502010-09-21 17:32:39 +00001135
1136 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001137 size_type bucket_count() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001138 {
1139 return __bucket_list_.get_deleter().size();
1140 }
1141
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001142 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001143 iterator begin() _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001144 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001145 iterator end() _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001146 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001147 const_iterator begin() const _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001148 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001149 const_iterator end() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001150
1151 template <class _Key>
Howard Hinnant99acc502010-09-21 17:32:39 +00001152 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001153 size_type bucket(const _Key& __k) const
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00001154 {
1155 _LIBCPP_ASSERT(bucket_count() > 0,
1156 "unordered container::bucket(key) called when bucket_count() == 0");
1157 return __constrain_hash(hash_function()(__k), bucket_count());
1158 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001159
1160 template <class _Key>
1161 iterator find(const _Key& __x);
1162 template <class _Key>
1163 const_iterator find(const _Key& __x) const;
1164
Howard Hinnant99968442011-11-29 18:15:50 +00001165 typedef __hash_node_destructor<__node_allocator> _Dp;
1166 typedef unique_ptr<__node, _Dp> __node_holder;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001167
1168 iterator erase(const_iterator __p);
1169 iterator erase(const_iterator __first, const_iterator __last);
1170 template <class _Key>
1171 size_type __erase_unique(const _Key& __k);
1172 template <class _Key>
1173 size_type __erase_multi(const _Key& __k);
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001174 __node_holder remove(const_iterator __p) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001175
1176 template <class _Key>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001177 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001178 size_type __count_unique(const _Key& __k) const;
1179 template <class _Key>
1180 size_type __count_multi(const _Key& __k) const;
1181
1182 template <class _Key>
1183 pair<iterator, iterator>
1184 __equal_range_unique(const _Key& __k);
1185 template <class _Key>
1186 pair<const_iterator, const_iterator>
1187 __equal_range_unique(const _Key& __k) const;
1188
1189 template <class _Key>
1190 pair<iterator, iterator>
1191 __equal_range_multi(const _Key& __k);
1192 template <class _Key>
1193 pair<const_iterator, const_iterator>
1194 __equal_range_multi(const _Key& __k) const;
1195
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001196 void swap(__hash_table& __u)
Eric Fiselier692177d2015-07-18 20:40:46 +00001197#if _LIBCPP_STD_VER <= 11
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001198 _NOEXCEPT_(
Marshall Clow7d914d12015-07-13 20:04:56 +00001199 __is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value
Marshall Clow7d914d12015-07-13 20:04:56 +00001200 && (!allocator_traits<__pointer_allocator>::propagate_on_container_swap::value
1201 || __is_nothrow_swappable<__pointer_allocator>::value)
1202 && (!__node_traits::propagate_on_container_swap::value
1203 || __is_nothrow_swappable<__node_allocator>::value)
Marshall Clow7d914d12015-07-13 20:04:56 +00001204 );
Eric Fiselier692177d2015-07-18 20:40:46 +00001205#else
1206 _NOEXCEPT_(__is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value);
1207#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001208
Howard Hinnant99acc502010-09-21 17:32:39 +00001209 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001210 size_type max_bucket_count() const _NOEXCEPT
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001211 {return __pointer_alloc_traits::max_size(__bucket_list_.get_deleter().__alloc());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001212 size_type bucket_size(size_type __n) const;
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001213 _LIBCPP_INLINE_VISIBILITY float load_factor() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001214 {
1215 size_type __bc = bucket_count();
1216 return __bc != 0 ? (float)size() / __bc : 0.f;
1217 }
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001218 _LIBCPP_INLINE_VISIBILITY void max_load_factor(float __mlf) _NOEXCEPT
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00001219 {
1220 _LIBCPP_ASSERT(__mlf > 0,
1221 "unordered container::max_load_factor(lf) called with lf <= 0");
1222 max_load_factor() = _VSTD::max(__mlf, load_factor());
1223 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001224
Howard Hinnant39213642013-07-23 22:01:58 +00001225 _LIBCPP_INLINE_VISIBILITY
1226 local_iterator
1227 begin(size_type __n)
1228 {
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00001229 _LIBCPP_ASSERT(__n < bucket_count(),
1230 "unordered container::begin(n) called with n >= bucket_count()");
Howard Hinnant39213642013-07-23 22:01:58 +00001231#if _LIBCPP_DEBUG_LEVEL >= 2
1232 return local_iterator(__bucket_list_[__n], __n, bucket_count(), this);
1233#else
1234 return local_iterator(__bucket_list_[__n], __n, bucket_count());
1235#endif
1236 }
1237
1238 _LIBCPP_INLINE_VISIBILITY
1239 local_iterator
1240 end(size_type __n)
1241 {
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00001242 _LIBCPP_ASSERT(__n < bucket_count(),
1243 "unordered container::end(n) called with n >= bucket_count()");
Howard Hinnant39213642013-07-23 22:01:58 +00001244#if _LIBCPP_DEBUG_LEVEL >= 2
1245 return local_iterator(nullptr, __n, bucket_count(), this);
1246#else
1247 return local_iterator(nullptr, __n, bucket_count());
1248#endif
1249 }
1250
1251 _LIBCPP_INLINE_VISIBILITY
1252 const_local_iterator
1253 cbegin(size_type __n) const
1254 {
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00001255 _LIBCPP_ASSERT(__n < bucket_count(),
1256 "unordered container::cbegin(n) called with n >= bucket_count()");
Howard Hinnant39213642013-07-23 22:01:58 +00001257#if _LIBCPP_DEBUG_LEVEL >= 2
1258 return const_local_iterator(__bucket_list_[__n], __n, bucket_count(), this);
1259#else
1260 return const_local_iterator(__bucket_list_[__n], __n, bucket_count());
1261#endif
1262 }
1263
1264 _LIBCPP_INLINE_VISIBILITY
1265 const_local_iterator
1266 cend(size_type __n) const
1267 {
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00001268 _LIBCPP_ASSERT(__n < bucket_count(),
1269 "unordered container::cend(n) called with n >= bucket_count()");
Howard Hinnant39213642013-07-23 22:01:58 +00001270#if _LIBCPP_DEBUG_LEVEL >= 2
1271 return const_local_iterator(nullptr, __n, bucket_count(), this);
1272#else
1273 return const_local_iterator(nullptr, __n, bucket_count());
1274#endif
1275 }
1276
1277#if _LIBCPP_DEBUG_LEVEL >= 2
1278
1279 bool __dereferenceable(const const_iterator* __i) const;
1280 bool __decrementable(const const_iterator* __i) const;
1281 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1282 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1283
1284#endif // _LIBCPP_DEBUG_LEVEL >= 2
1285
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001286private:
1287 void __rehash(size_type __n);
1288
Eric Fiselier2960ae22016-02-11 11:59:44 +00001289#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001290 template <class ..._Args>
Eric Fiselier2960ae22016-02-11 11:59:44 +00001291 __node_holder __construct_node(_Args&& ...__args);
1292
1293 template <class _First, class ..._Rest>
1294 __node_holder __construct_node_hash(size_t __hash, _First&& __f, _Rest&&... __rest);
1295#else // _LIBCPP_CXX03_LANG
1296 __node_holder __construct_node(const __container_value_type& __v);
1297 __node_holder __construct_node_hash(size_t __hash, const __container_value_type& __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001298#endif
Eric Fiselier2960ae22016-02-11 11:59:44 +00001299
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001300
Howard Hinnant99acc502010-09-21 17:32:39 +00001301 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001302 void __copy_assign_alloc(const __hash_table& __u)
1303 {__copy_assign_alloc(__u, integral_constant<bool,
1304 __node_traits::propagate_on_container_copy_assignment::value>());}
1305 void __copy_assign_alloc(const __hash_table& __u, true_type);
Howard Hinnant99acc502010-09-21 17:32:39 +00001306 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00001307 void __copy_assign_alloc(const __hash_table&, false_type) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001308
Eric Fiselier2960ae22016-02-11 11:59:44 +00001309#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001310 void __move_assign(__hash_table& __u, false_type);
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001311 void __move_assign(__hash_table& __u, true_type)
1312 _NOEXCEPT_(
1313 is_nothrow_move_assignable<__node_allocator>::value &&
1314 is_nothrow_move_assignable<hasher>::value &&
1315 is_nothrow_move_assignable<key_equal>::value);
1316 _LIBCPP_INLINE_VISIBILITY
1317 void __move_assign_alloc(__hash_table& __u)
1318 _NOEXCEPT_(
1319 !__node_traits::propagate_on_container_move_assignment::value ||
1320 (is_nothrow_move_assignable<__pointer_allocator>::value &&
1321 is_nothrow_move_assignable<__node_allocator>::value))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001322 {__move_assign_alloc(__u, integral_constant<bool,
1323 __node_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnant99acc502010-09-21 17:32:39 +00001324 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001325 void __move_assign_alloc(__hash_table& __u, true_type)
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001326 _NOEXCEPT_(
1327 is_nothrow_move_assignable<__pointer_allocator>::value &&
1328 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001329 {
1330 __bucket_list_.get_deleter().__alloc() =
Howard Hinnant0949eed2011-06-30 21:18:19 +00001331 _VSTD::move(__u.__bucket_list_.get_deleter().__alloc());
1332 __node_alloc() = _VSTD::move(__u.__node_alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001333 }
Howard Hinnant99acc502010-09-21 17:32:39 +00001334 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001335 void __move_assign_alloc(__hash_table&, false_type) _NOEXCEPT {}
Eric Fiselier2960ae22016-02-11 11:59:44 +00001336#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001337
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001338 void __deallocate(__node_pointer __np) _NOEXCEPT;
1339 __node_pointer __detach() _NOEXCEPT;
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001340
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001341 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_map;
1342 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_multimap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001343};
1344
1345template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001346inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001347__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table()
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001348 _NOEXCEPT_(
1349 is_nothrow_default_constructible<__bucket_list>::value &&
1350 is_nothrow_default_constructible<__first_node>::value &&
Eric Fiselierc8f54c22015-12-16 00:53:04 +00001351 is_nothrow_default_constructible<__node_allocator>::value &&
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001352 is_nothrow_default_constructible<hasher>::value &&
1353 is_nothrow_default_constructible<key_equal>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001354 : __p2_(0),
1355 __p3_(1.0f)
1356{
1357}
1358
1359template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001360inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001361__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const hasher& __hf,
1362 const key_equal& __eql)
1363 : __bucket_list_(nullptr, __bucket_list_deleter()),
1364 __p1_(),
1365 __p2_(0, __hf),
1366 __p3_(1.0f, __eql)
1367{
1368}
1369
1370template <class _Tp, class _Hash, class _Equal, class _Alloc>
1371__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const hasher& __hf,
1372 const key_equal& __eql,
1373 const allocator_type& __a)
1374 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1375 __p1_(__node_allocator(__a)),
1376 __p2_(0, __hf),
1377 __p3_(1.0f, __eql)
1378{
1379}
1380
1381template <class _Tp, class _Hash, class _Equal, class _Alloc>
1382__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const allocator_type& __a)
1383 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1384 __p1_(__node_allocator(__a)),
1385 __p2_(0),
1386 __p3_(1.0f)
1387{
1388}
1389
1390template <class _Tp, class _Hash, class _Equal, class _Alloc>
1391__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const __hash_table& __u)
1392 : __bucket_list_(nullptr,
1393 __bucket_list_deleter(allocator_traits<__pointer_allocator>::
1394 select_on_container_copy_construction(
1395 __u.__bucket_list_.get_deleter().__alloc()), 0)),
1396 __p1_(allocator_traits<__node_allocator>::
1397 select_on_container_copy_construction(__u.__node_alloc())),
1398 __p2_(0, __u.hash_function()),
1399 __p3_(__u.__p3_)
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 const allocator_type& __a)
1406 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1407 __p1_(__node_allocator(__a)),
1408 __p2_(0, __u.hash_function()),
1409 __p3_(__u.__p3_)
1410{
1411}
1412
Eric Fiselier2960ae22016-02-11 11:59:44 +00001413#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001414
1415template <class _Tp, class _Hash, class _Equal, class _Alloc>
1416__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(__hash_table&& __u)
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001417 _NOEXCEPT_(
1418 is_nothrow_move_constructible<__bucket_list>::value &&
1419 is_nothrow_move_constructible<__first_node>::value &&
Eric Fiselierc8f54c22015-12-16 00:53:04 +00001420 is_nothrow_move_constructible<__node_allocator>::value &&
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001421 is_nothrow_move_constructible<hasher>::value &&
1422 is_nothrow_move_constructible<key_equal>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001423 : __bucket_list_(_VSTD::move(__u.__bucket_list_)),
1424 __p1_(_VSTD::move(__u.__p1_)),
1425 __p2_(_VSTD::move(__u.__p2_)),
1426 __p3_(_VSTD::move(__u.__p3_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001427{
1428 if (size() > 0)
1429 {
Howard Hinnant7a445152012-07-06 17:31:14 +00001430 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash_, bucket_count())] =
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001431 static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001432 __u.__p1_.first().__next_ = nullptr;
1433 __u.size() = 0;
1434 }
1435}
1436
1437template <class _Tp, class _Hash, class _Equal, class _Alloc>
1438__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(__hash_table&& __u,
1439 const allocator_type& __a)
1440 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1441 __p1_(__node_allocator(__a)),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001442 __p2_(0, _VSTD::move(__u.hash_function())),
1443 __p3_(_VSTD::move(__u.__p3_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001444{
1445 if (__a == allocator_type(__u.__node_alloc()))
1446 {
1447 __bucket_list_.reset(__u.__bucket_list_.release());
1448 __bucket_list_.get_deleter().size() = __u.__bucket_list_.get_deleter().size();
1449 __u.__bucket_list_.get_deleter().size() = 0;
1450 if (__u.size() > 0)
1451 {
1452 __p1_.first().__next_ = __u.__p1_.first().__next_;
1453 __u.__p1_.first().__next_ = nullptr;
Howard Hinnant7a445152012-07-06 17:31:14 +00001454 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash_, bucket_count())] =
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001455 static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001456 size() = __u.size();
1457 __u.size() = 0;
1458 }
1459 }
1460}
1461
Eric Fiselier2960ae22016-02-11 11:59:44 +00001462#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001463
1464template <class _Tp, class _Hash, class _Equal, class _Alloc>
1465__hash_table<_Tp, _Hash, _Equal, _Alloc>::~__hash_table()
1466{
1467 __deallocate(__p1_.first().__next_);
Howard Hinnant39213642013-07-23 22:01:58 +00001468#if _LIBCPP_DEBUG_LEVEL >= 2
1469 __get_db()->__erase_c(this);
1470#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001471}
1472
1473template <class _Tp, class _Hash, class _Equal, class _Alloc>
1474void
1475__hash_table<_Tp, _Hash, _Equal, _Alloc>::__copy_assign_alloc(
1476 const __hash_table& __u, true_type)
1477{
1478 if (__node_alloc() != __u.__node_alloc())
1479 {
1480 clear();
1481 __bucket_list_.reset();
1482 __bucket_list_.get_deleter().size() = 0;
1483 }
1484 __bucket_list_.get_deleter().__alloc() = __u.__bucket_list_.get_deleter().__alloc();
1485 __node_alloc() = __u.__node_alloc();
1486}
1487
1488template <class _Tp, class _Hash, class _Equal, class _Alloc>
1489__hash_table<_Tp, _Hash, _Equal, _Alloc>&
1490__hash_table<_Tp, _Hash, _Equal, _Alloc>::operator=(const __hash_table& __u)
1491{
1492 if (this != &__u)
1493 {
1494 __copy_assign_alloc(__u);
1495 hash_function() = __u.hash_function();
1496 key_eq() = __u.key_eq();
1497 max_load_factor() = __u.max_load_factor();
1498 __assign_multi(__u.begin(), __u.end());
1499 }
1500 return *this;
1501}
1502
1503template <class _Tp, class _Hash, class _Equal, class _Alloc>
1504void
1505__hash_table<_Tp, _Hash, _Equal, _Alloc>::__deallocate(__node_pointer __np)
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001506 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001507{
1508 __node_allocator& __na = __node_alloc();
1509 while (__np != nullptr)
1510 {
1511 __node_pointer __next = __np->__next_;
Howard Hinnant39213642013-07-23 22:01:58 +00001512#if _LIBCPP_DEBUG_LEVEL >= 2
1513 __c_node* __c = __get_db()->__find_c_and_lock(this);
1514 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1515 {
1516 --__p;
1517 iterator* __i = static_cast<iterator*>((*__p)->__i_);
1518 if (__i->__node_ == __np)
1519 {
1520 (*__p)->__c_ = nullptr;
1521 if (--__c->end_ != __p)
1522 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1523 }
1524 }
1525 __get_db()->unlock();
1526#endif
Eric Fiselier2960ae22016-02-11 11:59:44 +00001527 __node_traits::destroy(__na, _NodeTypes::__get_ptr(__np->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001528 __node_traits::deallocate(__na, __np, 1);
1529 __np = __next;
1530 }
1531}
1532
1533template <class _Tp, class _Hash, class _Equal, class _Alloc>
1534typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_pointer
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001535__hash_table<_Tp, _Hash, _Equal, _Alloc>::__detach() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001536{
1537 size_type __bc = bucket_count();
1538 for (size_type __i = 0; __i < __bc; ++__i)
1539 __bucket_list_[__i] = nullptr;
1540 size() = 0;
1541 __node_pointer __cache = __p1_.first().__next_;
1542 __p1_.first().__next_ = nullptr;
1543 return __cache;
1544}
1545
Eric Fiselier2960ae22016-02-11 11:59:44 +00001546#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001547
1548template <class _Tp, class _Hash, class _Equal, class _Alloc>
1549void
1550__hash_table<_Tp, _Hash, _Equal, _Alloc>::__move_assign(
1551 __hash_table& __u, true_type)
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001552 _NOEXCEPT_(
1553 is_nothrow_move_assignable<__node_allocator>::value &&
1554 is_nothrow_move_assignable<hasher>::value &&
1555 is_nothrow_move_assignable<key_equal>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001556{
1557 clear();
1558 __bucket_list_.reset(__u.__bucket_list_.release());
1559 __bucket_list_.get_deleter().size() = __u.__bucket_list_.get_deleter().size();
1560 __u.__bucket_list_.get_deleter().size() = 0;
1561 __move_assign_alloc(__u);
1562 size() = __u.size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001563 hash_function() = _VSTD::move(__u.hash_function());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001564 max_load_factor() = __u.max_load_factor();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001565 key_eq() = _VSTD::move(__u.key_eq());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001566 __p1_.first().__next_ = __u.__p1_.first().__next_;
1567 if (size() > 0)
1568 {
Howard Hinnant7a445152012-07-06 17:31:14 +00001569 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash_, bucket_count())] =
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001570 static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001571 __u.__p1_.first().__next_ = nullptr;
1572 __u.size() = 0;
1573 }
Howard Hinnant39213642013-07-23 22:01:58 +00001574#if _LIBCPP_DEBUG_LEVEL >= 2
1575 __get_db()->swap(this, &__u);
1576#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001577}
1578
1579template <class _Tp, class _Hash, class _Equal, class _Alloc>
1580void
1581__hash_table<_Tp, _Hash, _Equal, _Alloc>::__move_assign(
1582 __hash_table& __u, false_type)
1583{
1584 if (__node_alloc() == __u.__node_alloc())
1585 __move_assign(__u, true_type());
1586 else
1587 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001588 hash_function() = _VSTD::move(__u.hash_function());
1589 key_eq() = _VSTD::move(__u.key_eq());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001590 max_load_factor() = __u.max_load_factor();
1591 if (bucket_count() != 0)
1592 {
1593 __node_pointer __cache = __detach();
1594#ifndef _LIBCPP_NO_EXCEPTIONS
1595 try
1596 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001597#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001598 const_iterator __i = __u.begin();
1599 while (__cache != nullptr && __u.size() != 0)
1600 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001601 __cache->__value_ = _VSTD::move(__u.remove(__i++)->__value_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001602 __node_pointer __next = __cache->__next_;
1603 __node_insert_multi(__cache);
1604 __cache = __next;
1605 }
1606#ifndef _LIBCPP_NO_EXCEPTIONS
1607 }
1608 catch (...)
1609 {
1610 __deallocate(__cache);
1611 throw;
1612 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001613#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001614 __deallocate(__cache);
1615 }
1616 const_iterator __i = __u.begin();
1617 while (__u.size() != 0)
1618 {
Eric Fiselier2960ae22016-02-11 11:59:44 +00001619 __node_holder __h = __construct_node(_NodeTypes::__move(__u.remove(__i++)->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001620 __node_insert_multi(__h.get());
1621 __h.release();
1622 }
1623 }
1624}
1625
1626template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001627inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001628__hash_table<_Tp, _Hash, _Equal, _Alloc>&
1629__hash_table<_Tp, _Hash, _Equal, _Alloc>::operator=(__hash_table&& __u)
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001630 _NOEXCEPT_(
1631 __node_traits::propagate_on_container_move_assignment::value &&
1632 is_nothrow_move_assignable<__node_allocator>::value &&
1633 is_nothrow_move_assignable<hasher>::value &&
1634 is_nothrow_move_assignable<key_equal>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001635{
1636 __move_assign(__u, integral_constant<bool,
1637 __node_traits::propagate_on_container_move_assignment::value>());
1638 return *this;
1639}
1640
Eric Fiselier2960ae22016-02-11 11:59:44 +00001641#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001642
1643template <class _Tp, class _Hash, class _Equal, class _Alloc>
1644template <class _InputIterator>
1645void
1646__hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_unique(_InputIterator __first,
1647 _InputIterator __last)
1648{
Eric Fiselier2960ae22016-02-11 11:59:44 +00001649 typedef iterator_traits<_InputIterator> _ITraits;
1650 typedef typename _ITraits::value_type _ItValueType;
1651 static_assert((is_same<_ItValueType, __container_value_type>::value),
1652 "__assign_unique may only be called with the containers value type");
1653
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001654 if (bucket_count() != 0)
1655 {
1656 __node_pointer __cache = __detach();
1657#ifndef _LIBCPP_NO_EXCEPTIONS
1658 try
1659 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001660#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001661 for (; __cache != nullptr && __first != __last; ++__first)
1662 {
1663 __cache->__value_ = *__first;
1664 __node_pointer __next = __cache->__next_;
1665 __node_insert_unique(__cache);
1666 __cache = __next;
1667 }
1668#ifndef _LIBCPP_NO_EXCEPTIONS
1669 }
1670 catch (...)
1671 {
1672 __deallocate(__cache);
1673 throw;
1674 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001675#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001676 __deallocate(__cache);
1677 }
1678 for (; __first != __last; ++__first)
1679 __insert_unique(*__first);
1680}
1681
1682template <class _Tp, class _Hash, class _Equal, class _Alloc>
1683template <class _InputIterator>
1684void
1685__hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_multi(_InputIterator __first,
1686 _InputIterator __last)
1687{
Eric Fiselier2960ae22016-02-11 11:59:44 +00001688 typedef iterator_traits<_InputIterator> _ITraits;
1689 typedef typename _ITraits::value_type _ItValueType;
1690 static_assert((is_same<_ItValueType, __container_value_type>::value ||
1691 is_same<_ItValueType, __node_value_type>::value),
1692 "__assign_multi may only be called with the containers value type"
1693 " or the nodes value type");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001694 if (bucket_count() != 0)
1695 {
1696 __node_pointer __cache = __detach();
1697#ifndef _LIBCPP_NO_EXCEPTIONS
1698 try
1699 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001700#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001701 for (; __cache != nullptr && __first != __last; ++__first)
1702 {
1703 __cache->__value_ = *__first;
1704 __node_pointer __next = __cache->__next_;
1705 __node_insert_multi(__cache);
1706 __cache = __next;
1707 }
1708#ifndef _LIBCPP_NO_EXCEPTIONS
1709 }
1710 catch (...)
1711 {
1712 __deallocate(__cache);
1713 throw;
1714 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001715#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001716 __deallocate(__cache);
1717 }
1718 for (; __first != __last; ++__first)
Eric Fiselier2960ae22016-02-11 11:59:44 +00001719 __insert_multi(_NodeTypes::__get_value(*__first));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001720}
1721
1722template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001723inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001724typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001725__hash_table<_Tp, _Hash, _Equal, _Alloc>::begin() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001726{
Howard Hinnant39213642013-07-23 22:01:58 +00001727#if _LIBCPP_DEBUG_LEVEL >= 2
1728 return iterator(__p1_.first().__next_, this);
1729#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001730 return iterator(__p1_.first().__next_);
Howard Hinnant39213642013-07-23 22:01:58 +00001731#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001732}
1733
1734template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001735inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001736typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001737__hash_table<_Tp, _Hash, _Equal, _Alloc>::end() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001738{
Howard Hinnant39213642013-07-23 22:01:58 +00001739#if _LIBCPP_DEBUG_LEVEL >= 2
1740 return iterator(nullptr, this);
1741#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001742 return iterator(nullptr);
Howard Hinnant39213642013-07-23 22:01:58 +00001743#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001744}
1745
1746template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001747inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001748typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001749__hash_table<_Tp, _Hash, _Equal, _Alloc>::begin() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001750{
Howard Hinnant39213642013-07-23 22:01:58 +00001751#if _LIBCPP_DEBUG_LEVEL >= 2
1752 return const_iterator(__p1_.first().__next_, this);
1753#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001754 return const_iterator(__p1_.first().__next_);
Howard Hinnant39213642013-07-23 22:01:58 +00001755#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001756}
1757
1758template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001759inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001760typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001761__hash_table<_Tp, _Hash, _Equal, _Alloc>::end() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001762{
Howard Hinnant39213642013-07-23 22:01:58 +00001763#if _LIBCPP_DEBUG_LEVEL >= 2
1764 return const_iterator(nullptr, this);
1765#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001766 return const_iterator(nullptr);
Howard Hinnant39213642013-07-23 22:01:58 +00001767#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001768}
1769
1770template <class _Tp, class _Hash, class _Equal, class _Alloc>
1771void
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001772__hash_table<_Tp, _Hash, _Equal, _Alloc>::clear() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001773{
1774 if (size() > 0)
1775 {
1776 __deallocate(__p1_.first().__next_);
1777 __p1_.first().__next_ = nullptr;
1778 size_type __bc = bucket_count();
Howard Hinnant9f66bff2011-07-05 14:14:17 +00001779 for (size_type __i = 0; __i < __bc; ++__i)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001780 __bucket_list_[__i] = nullptr;
1781 size() = 0;
1782 }
1783}
1784
1785template <class _Tp, class _Hash, class _Equal, class _Alloc>
1786pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
1787__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_unique(__node_pointer __nd)
1788{
1789 __nd->__hash_ = hash_function()(__nd->__value_);
1790 size_type __bc = bucket_count();
1791 bool __inserted = false;
1792 __node_pointer __ndptr;
1793 size_t __chash;
1794 if (__bc != 0)
1795 {
Howard Hinnant7a445152012-07-06 17:31:14 +00001796 __chash = __constrain_hash(__nd->__hash_, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001797 __ndptr = __bucket_list_[__chash];
1798 if (__ndptr != nullptr)
1799 {
1800 for (__ndptr = __ndptr->__next_; __ndptr != nullptr &&
Howard Hinnant7a445152012-07-06 17:31:14 +00001801 __constrain_hash(__ndptr->__hash_, __bc) == __chash;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001802 __ndptr = __ndptr->__next_)
1803 {
1804 if (key_eq()(__ndptr->__value_, __nd->__value_))
1805 goto __done;
1806 }
1807 }
1808 }
1809 {
1810 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1811 {
Eric Fiselier57947ca2015-02-02 21:31:48 +00001812 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001813 size_type(ceil(float(size() + 1) / max_load_factor()))));
1814 __bc = bucket_count();
Howard Hinnant7a445152012-07-06 17:31:14 +00001815 __chash = __constrain_hash(__nd->__hash_, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001816 }
1817 // insert_after __bucket_list_[__chash], or __first_node if bucket is null
1818 __node_pointer __pn = __bucket_list_[__chash];
1819 if (__pn == nullptr)
1820 {
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001821 __pn = static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001822 __nd->__next_ = __pn->__next_;
1823 __pn->__next_ = __nd;
1824 // fix up __bucket_list_
1825 __bucket_list_[__chash] = __pn;
1826 if (__nd->__next_ != nullptr)
Howard Hinnant7a445152012-07-06 17:31:14 +00001827 __bucket_list_[__constrain_hash(__nd->__next_->__hash_, __bc)] = __nd;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001828 }
1829 else
1830 {
1831 __nd->__next_ = __pn->__next_;
1832 __pn->__next_ = __nd;
1833 }
1834 __ndptr = __nd;
1835 // increment size
1836 ++size();
1837 __inserted = true;
1838 }
1839__done:
Howard Hinnant39213642013-07-23 22:01:58 +00001840#if _LIBCPP_DEBUG_LEVEL >= 2
1841 return pair<iterator, bool>(iterator(__ndptr, this), __inserted);
1842#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001843 return pair<iterator, bool>(iterator(__ndptr), __inserted);
Howard Hinnant39213642013-07-23 22:01:58 +00001844#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001845}
1846
1847template <class _Tp, class _Hash, class _Equal, class _Alloc>
1848typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1849__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi(__node_pointer __cp)
1850{
1851 __cp->__hash_ = hash_function()(__cp->__value_);
1852 size_type __bc = bucket_count();
1853 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1854 {
Eric Fiselier57947ca2015-02-02 21:31:48 +00001855 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001856 size_type(ceil(float(size() + 1) / max_load_factor()))));
1857 __bc = bucket_count();
1858 }
Howard Hinnant7a445152012-07-06 17:31:14 +00001859 size_t __chash = __constrain_hash(__cp->__hash_, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001860 __node_pointer __pn = __bucket_list_[__chash];
1861 if (__pn == nullptr)
1862 {
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001863 __pn = static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001864 __cp->__next_ = __pn->__next_;
1865 __pn->__next_ = __cp;
1866 // fix up __bucket_list_
1867 __bucket_list_[__chash] = __pn;
1868 if (__cp->__next_ != nullptr)
Howard Hinnant7a445152012-07-06 17:31:14 +00001869 __bucket_list_[__constrain_hash(__cp->__next_->__hash_, __bc)] = __cp;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001870 }
1871 else
1872 {
1873 for (bool __found = false; __pn->__next_ != nullptr &&
Howard Hinnant7a445152012-07-06 17:31:14 +00001874 __constrain_hash(__pn->__next_->__hash_, __bc) == __chash;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001875 __pn = __pn->__next_)
Howard Hinnant324bb032010-08-22 00:02:43 +00001876 {
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001877 // __found key_eq() action
1878 // false false loop
1879 // true true loop
1880 // false true set __found to true
1881 // true false break
1882 if (__found != (__pn->__next_->__hash_ == __cp->__hash_ &&
1883 key_eq()(__pn->__next_->__value_, __cp->__value_)))
1884 {
1885 if (!__found)
1886 __found = true;
1887 else
1888 break;
1889 }
1890 }
1891 __cp->__next_ = __pn->__next_;
1892 __pn->__next_ = __cp;
1893 if (__cp->__next_ != nullptr)
1894 {
Howard Hinnant7a445152012-07-06 17:31:14 +00001895 size_t __nhash = __constrain_hash(__cp->__next_->__hash_, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001896 if (__nhash != __chash)
1897 __bucket_list_[__nhash] = __cp;
1898 }
1899 }
1900 ++size();
Howard Hinnant39213642013-07-23 22:01:58 +00001901#if _LIBCPP_DEBUG_LEVEL >= 2
1902 return iterator(__cp, this);
1903#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001904 return iterator(__cp);
Howard Hinnant39213642013-07-23 22:01:58 +00001905#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001906}
1907
1908template <class _Tp, class _Hash, class _Equal, class _Alloc>
1909typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1910__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi(
1911 const_iterator __p, __node_pointer __cp)
1912{
Howard Hinnant824c1992013-08-02 17:50:49 +00001913#if _LIBCPP_DEBUG_LEVEL >= 2
1914 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1915 "unordered container::emplace_hint(const_iterator, args...) called with an iterator not"
1916 " referring to this unordered container");
1917#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001918 if (__p != end() && key_eq()(*__p, __cp->__value_))
1919 {
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001920 __node_pointer __np = __p.__node_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001921 __cp->__hash_ = __np->__hash_;
1922 size_type __bc = bucket_count();
1923 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1924 {
Eric Fiselier57947ca2015-02-02 21:31:48 +00001925 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001926 size_type(ceil(float(size() + 1) / max_load_factor()))));
1927 __bc = bucket_count();
1928 }
Howard Hinnant7a445152012-07-06 17:31:14 +00001929 size_t __chash = __constrain_hash(__cp->__hash_, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001930 __node_pointer __pp = __bucket_list_[__chash];
1931 while (__pp->__next_ != __np)
1932 __pp = __pp->__next_;
1933 __cp->__next_ = __np;
1934 __pp->__next_ = __cp;
1935 ++size();
Howard Hinnant39213642013-07-23 22:01:58 +00001936#if _LIBCPP_DEBUG_LEVEL >= 2
1937 return iterator(__cp, this);
1938#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001939 return iterator(__cp);
Howard Hinnant39213642013-07-23 22:01:58 +00001940#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001941 }
1942 return __node_insert_multi(__cp);
1943}
1944
Eric Fiselierfdae69a2015-06-13 07:18:32 +00001945
1946
Eric Fiselier2960ae22016-02-11 11:59:44 +00001947#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierfdae69a2015-06-13 07:18:32 +00001948template <class _Tp, class _Hash, class _Equal, class _Alloc>
Eric Fiselier2960ae22016-02-11 11:59:44 +00001949template <class _Key, class ..._Args>
Eric Fiselierfdae69a2015-06-13 07:18:32 +00001950_LIBCPP_INLINE_VISIBILITY
1951pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
Eric Fiselier2960ae22016-02-11 11:59:44 +00001952__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_key_args(_Key const& __k, _Args&&... __args)
Eric Fiselierfdae69a2015-06-13 07:18:32 +00001953#else
1954template <class _Tp, class _Hash, class _Equal, class _Alloc>
Eric Fiselier2960ae22016-02-11 11:59:44 +00001955template <class _Key, class _Args>
Eric Fiselierfdae69a2015-06-13 07:18:32 +00001956_LIBCPP_INLINE_VISIBILITY
1957pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
Eric Fiselier2960ae22016-02-11 11:59:44 +00001958__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_key_args(_Key const& __k, _Args& __args)
Eric Fiselierfdae69a2015-06-13 07:18:32 +00001959#endif
1960{
Eric Fiselier2960ae22016-02-11 11:59:44 +00001961
1962 size_t __hash = hash_function()(__k);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001963 size_type __bc = bucket_count();
1964 bool __inserted = false;
1965 __node_pointer __nd;
1966 size_t __chash;
1967 if (__bc != 0)
1968 {
Howard Hinnant7a445152012-07-06 17:31:14 +00001969 __chash = __constrain_hash(__hash, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001970 __nd = __bucket_list_[__chash];
1971 if (__nd != nullptr)
1972 {
1973 for (__nd = __nd->__next_; __nd != nullptr &&
Howard Hinnant7a445152012-07-06 17:31:14 +00001974 __constrain_hash(__nd->__hash_, __bc) == __chash;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001975 __nd = __nd->__next_)
1976 {
Eric Fiselier2960ae22016-02-11 11:59:44 +00001977 if (key_eq()(__nd->__value_, __k))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001978 goto __done;
1979 }
1980 }
1981 }
1982 {
Eric Fiselier2960ae22016-02-11 11:59:44 +00001983#ifndef _LIBCPP_CXX03_LANG
1984 __node_holder __h = __construct_node_hash(__hash, _VSTD::forward<_Args>(__args)...);
1985#else
1986 __node_holder __h = __construct_node_hash(__hash, __args);
1987#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001988 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1989 {
Eric Fiselier57947ca2015-02-02 21:31:48 +00001990 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001991 size_type(ceil(float(size() + 1) / max_load_factor()))));
1992 __bc = bucket_count();
Howard Hinnant7a445152012-07-06 17:31:14 +00001993 __chash = __constrain_hash(__hash, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001994 }
1995 // insert_after __bucket_list_[__chash], or __first_node if bucket is null
1996 __node_pointer __pn = __bucket_list_[__chash];
1997 if (__pn == nullptr)
1998 {
Eric Fiselier9e9f42e2016-02-11 15:22:37 +00001999 __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 +00002000 __h->__next_ = __pn->__next_;
2001 __pn->__next_ = __h.get();
2002 // fix up __bucket_list_
2003 __bucket_list_[__chash] = __pn;
2004 if (__h->__next_ != nullptr)
Howard Hinnant7a445152012-07-06 17:31:14 +00002005 __bucket_list_[__constrain_hash(__h->__next_->__hash_, __bc)] = __h.get();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002006 }
2007 else
2008 {
2009 __h->__next_ = __pn->__next_;
2010 __pn->__next_ = __h.get();
2011 }
2012 __nd = __h.release();
2013 // increment size
2014 ++size();
2015 __inserted = true;
2016 }
2017__done:
Howard Hinnant39213642013-07-23 22:01:58 +00002018#if _LIBCPP_DEBUG_LEVEL >= 2
2019 return pair<iterator, bool>(iterator(__nd, this), __inserted);
2020#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002021 return pair<iterator, bool>(iterator(__nd), __inserted);
Howard Hinnant39213642013-07-23 22:01:58 +00002022#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002023}
2024
Eric Fiselier2960ae22016-02-11 11:59:44 +00002025#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002026
2027template <class _Tp, class _Hash, class _Equal, class _Alloc>
2028template <class... _Args>
2029pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
Duncan P. N. Exon Smith798ec842016-03-17 20:45:20 +00002030__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_impl(_Args&&... __args)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002031{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002032 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002033 pair<iterator, bool> __r = __node_insert_unique(__h.get());
2034 if (__r.second)
2035 __h.release();
2036 return __r;
2037}
2038
2039template <class _Tp, class _Hash, class _Equal, class _Alloc>
2040template <class... _Args>
2041typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2042__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_multi(_Args&&... __args)
2043{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002044 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002045 iterator __r = __node_insert_multi(__h.get());
2046 __h.release();
2047 return __r;
2048}
2049
2050template <class _Tp, class _Hash, class _Equal, class _Alloc>
2051template <class... _Args>
2052typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2053__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_hint_multi(
2054 const_iterator __p, _Args&&... __args)
2055{
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00002056#if _LIBCPP_DEBUG_LEVEL >= 2
2057 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2058 "unordered container::emplace_hint(const_iterator, args...) called with an iterator not"
2059 " referring to this unordered container");
2060#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00002061 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002062 iterator __r = __node_insert_multi(__p, __h.get());
2063 __h.release();
2064 return __r;
2065}
2066
Eric Fiselier2960ae22016-02-11 11:59:44 +00002067#else // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002068
2069template <class _Tp, class _Hash, class _Equal, class _Alloc>
2070typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
Eric Fiselier2960ae22016-02-11 11:59:44 +00002071__hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const __container_value_type& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002072{
2073 __node_holder __h = __construct_node(__x);
2074 iterator __r = __node_insert_multi(__h.get());
2075 __h.release();
2076 return __r;
2077}
2078
2079template <class _Tp, class _Hash, class _Equal, class _Alloc>
2080typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2081__hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const_iterator __p,
Eric Fiselier2960ae22016-02-11 11:59:44 +00002082 const __container_value_type& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002083{
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00002084#if _LIBCPP_DEBUG_LEVEL >= 2
2085 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2086 "unordered container::insert(const_iterator, lvalue) called with an iterator not"
2087 " referring to this unordered container");
2088#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002089 __node_holder __h = __construct_node(__x);
2090 iterator __r = __node_insert_multi(__p, __h.get());
2091 __h.release();
2092 return __r;
2093}
2094
Eric Fiselier2960ae22016-02-11 11:59:44 +00002095#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002096
2097template <class _Tp, class _Hash, class _Equal, class _Alloc>
2098void
2099__hash_table<_Tp, _Hash, _Equal, _Alloc>::rehash(size_type __n)
2100{
Howard Hinnant7a445152012-07-06 17:31:14 +00002101 if (__n == 1)
2102 __n = 2;
2103 else if (__n & (__n - 1))
2104 __n = __next_prime(__n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002105 size_type __bc = bucket_count();
2106 if (__n > __bc)
2107 __rehash(__n);
Howard Hinnant7a445152012-07-06 17:31:14 +00002108 else if (__n < __bc)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002109 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002110 __n = _VSTD::max<size_type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002111 (
2112 __n,
Eric Fiselier57947ca2015-02-02 21:31:48 +00002113 __is_hash_power2(__bc) ? __next_hash_pow2(size_t(ceil(float(size()) / max_load_factor()))) :
2114 __next_prime(size_t(ceil(float(size()) / max_load_factor())))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002115 );
2116 if (__n < __bc)
2117 __rehash(__n);
2118 }
2119}
2120
2121template <class _Tp, class _Hash, class _Equal, class _Alloc>
2122void
2123__hash_table<_Tp, _Hash, _Equal, _Alloc>::__rehash(size_type __nbc)
2124{
Howard Hinnant39213642013-07-23 22:01:58 +00002125#if _LIBCPP_DEBUG_LEVEL >= 2
2126 __get_db()->__invalidate_all(this);
2127#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002128 __pointer_allocator& __npa = __bucket_list_.get_deleter().__alloc();
2129 __bucket_list_.reset(__nbc > 0 ?
2130 __pointer_alloc_traits::allocate(__npa, __nbc) : nullptr);
2131 __bucket_list_.get_deleter().size() = __nbc;
2132 if (__nbc > 0)
2133 {
2134 for (size_type __i = 0; __i < __nbc; ++__i)
2135 __bucket_list_[__i] = nullptr;
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00002136 __node_pointer __pp(static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first())));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002137 __node_pointer __cp = __pp->__next_;
2138 if (__cp != nullptr)
2139 {
Howard Hinnant7a445152012-07-06 17:31:14 +00002140 size_type __chash = __constrain_hash(__cp->__hash_, __nbc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002141 __bucket_list_[__chash] = __pp;
2142 size_type __phash = __chash;
2143 for (__pp = __cp, __cp = __cp->__next_; __cp != nullptr;
2144 __cp = __pp->__next_)
2145 {
Howard Hinnant7a445152012-07-06 17:31:14 +00002146 __chash = __constrain_hash(__cp->__hash_, __nbc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002147 if (__chash == __phash)
2148 __pp = __cp;
2149 else
2150 {
2151 if (__bucket_list_[__chash] == nullptr)
2152 {
2153 __bucket_list_[__chash] = __pp;
2154 __pp = __cp;
2155 __phash = __chash;
2156 }
2157 else
2158 {
2159 __node_pointer __np = __cp;
2160 for (; __np->__next_ != nullptr &&
2161 key_eq()(__cp->__value_, __np->__next_->__value_);
2162 __np = __np->__next_)
2163 ;
2164 __pp->__next_ = __np->__next_;
2165 __np->__next_ = __bucket_list_[__chash]->__next_;
2166 __bucket_list_[__chash]->__next_ = __cp;
Howard Hinnant324bb032010-08-22 00:02:43 +00002167
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002168 }
2169 }
2170 }
2171 }
2172 }
2173}
2174
2175template <class _Tp, class _Hash, class _Equal, class _Alloc>
2176template <class _Key>
2177typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2178__hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k)
2179{
2180 size_t __hash = hash_function()(__k);
2181 size_type __bc = bucket_count();
2182 if (__bc != 0)
2183 {
Howard Hinnant7a445152012-07-06 17:31:14 +00002184 size_t __chash = __constrain_hash(__hash, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002185 __node_pointer __nd = __bucket_list_[__chash];
2186 if (__nd != nullptr)
2187 {
2188 for (__nd = __nd->__next_; __nd != nullptr &&
Howard Hinnant7a445152012-07-06 17:31:14 +00002189 __constrain_hash(__nd->__hash_, __bc) == __chash;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002190 __nd = __nd->__next_)
2191 {
2192 if (key_eq()(__nd->__value_, __k))
Howard Hinnant39213642013-07-23 22:01:58 +00002193#if _LIBCPP_DEBUG_LEVEL >= 2
2194 return iterator(__nd, this);
2195#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002196 return iterator(__nd);
Howard Hinnant39213642013-07-23 22:01:58 +00002197#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002198 }
2199 }
2200 }
2201 return end();
2202}
2203
2204template <class _Tp, class _Hash, class _Equal, class _Alloc>
2205template <class _Key>
2206typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
2207__hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k) const
2208{
2209 size_t __hash = hash_function()(__k);
2210 size_type __bc = bucket_count();
2211 if (__bc != 0)
2212 {
Howard Hinnant7a445152012-07-06 17:31:14 +00002213 size_t __chash = __constrain_hash(__hash, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002214 __node_const_pointer __nd = __bucket_list_[__chash];
2215 if (__nd != nullptr)
2216 {
2217 for (__nd = __nd->__next_; __nd != nullptr &&
Howard Hinnant7a445152012-07-06 17:31:14 +00002218 __constrain_hash(__nd->__hash_, __bc) == __chash;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002219 __nd = __nd->__next_)
2220 {
2221 if (key_eq()(__nd->__value_, __k))
Howard Hinnant39213642013-07-23 22:01:58 +00002222#if _LIBCPP_DEBUG_LEVEL >= 2
2223 return const_iterator(__nd, this);
2224#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002225 return const_iterator(__nd);
Howard Hinnant39213642013-07-23 22:01:58 +00002226#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002227 }
2228 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002229
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002230 }
2231 return end();
2232}
2233
Eric Fiselier2960ae22016-02-11 11:59:44 +00002234#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002235
2236template <class _Tp, class _Hash, class _Equal, class _Alloc>
2237template <class ..._Args>
2238typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
2239__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(_Args&& ...__args)
2240{
Eric Fiselier2960ae22016-02-11 11:59:44 +00002241 static_assert(!__is_hash_value_type<_Args...>::value,
2242 "Construct cannot be called with a hash value type");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002243 __node_allocator& __na = __node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00002244 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselier2960ae22016-02-11 11:59:44 +00002245 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002246 __h.get_deleter().__value_constructed = true;
2247 __h->__hash_ = hash_function()(__h->__value_);
2248 __h->__next_ = nullptr;
2249 return __h;
2250}
2251
2252template <class _Tp, class _Hash, class _Equal, class _Alloc>
Eric Fiselier2960ae22016-02-11 11:59:44 +00002253template <class _First, class ..._Rest>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002254typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Eric Fiselier2960ae22016-02-11 11:59:44 +00002255__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node_hash(
2256 size_t __hash, _First&& __f, _Rest&& ...__rest)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002257{
Eric Fiselier2960ae22016-02-11 11:59:44 +00002258 static_assert(!__is_hash_value_type<_First, _Rest...>::value,
2259 "Construct cannot be called with a hash value type");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002260 __node_allocator& __na = __node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00002261 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselier2960ae22016-02-11 11:59:44 +00002262 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_),
2263 _VSTD::forward<_First>(__f),
2264 _VSTD::forward<_Rest>(__rest)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002265 __h.get_deleter().__value_constructed = true;
2266 __h->__hash_ = __hash;
2267 __h->__next_ = nullptr;
Howard Hinnant9a894d92013-08-22 18:29:50 +00002268 return __h;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002269}
2270
Eric Fiselier2960ae22016-02-11 11:59:44 +00002271#else // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002272
2273template <class _Tp, class _Hash, class _Equal, class _Alloc>
2274typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Eric Fiselier2960ae22016-02-11 11:59:44 +00002275__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(const __container_value_type& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002276{
2277 __node_allocator& __na = __node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00002278 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselier2960ae22016-02-11 11:59:44 +00002279 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002280 __h.get_deleter().__value_constructed = true;
2281 __h->__hash_ = hash_function()(__h->__value_);
2282 __h->__next_ = nullptr;
Dimitry Andric89663502015-08-19 06:43:33 +00002283 return _LIBCPP_EXPLICIT_MOVE(__h); // explicitly moved for C++03
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002284}
2285
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002286template <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_hash(size_t __hash,
2289 const __container_value_type& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002290{
2291 __node_allocator& __na = __node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00002292 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselier2960ae22016-02-11 11:59:44 +00002293 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002294 __h.get_deleter().__value_constructed = true;
2295 __h->__hash_ = __hash;
2296 __h->__next_ = nullptr;
Dimitry Andric89663502015-08-19 06:43:33 +00002297 return _LIBCPP_EXPLICIT_MOVE(__h); // explicitly moved for C++03
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002298}
2299
Eric Fiselier2960ae22016-02-11 11:59:44 +00002300#endif // _LIBCPP_CXX03_LANG
2301
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002302template <class _Tp, class _Hash, class _Equal, class _Alloc>
2303typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2304__hash_table<_Tp, _Hash, _Equal, _Alloc>::erase(const_iterator __p)
2305{
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00002306 __node_pointer __np = __p.__node_;
Howard Hinnant39213642013-07-23 22:01:58 +00002307#if _LIBCPP_DEBUG_LEVEL >= 2
2308 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2309 "unordered container erase(iterator) called with an iterator not"
2310 " referring to this container");
2311 _LIBCPP_ASSERT(__p != end(),
2312 "unordered container erase(iterator) called with a non-dereferenceable iterator");
2313 iterator __r(__np, this);
2314#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002315 iterator __r(__np);
Howard Hinnant39213642013-07-23 22:01:58 +00002316#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002317 ++__r;
2318 remove(__p);
2319 return __r;
2320}
2321
2322template <class _Tp, class _Hash, class _Equal, class _Alloc>
2323typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2324__hash_table<_Tp, _Hash, _Equal, _Alloc>::erase(const_iterator __first,
2325 const_iterator __last)
2326{
Howard Hinnant39213642013-07-23 22:01:58 +00002327#if _LIBCPP_DEBUG_LEVEL >= 2
2328 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
2329 "unodered container::erase(iterator, iterator) called with an iterator not"
2330 " referring to this unodered container");
2331 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__last) == this,
2332 "unodered container::erase(iterator, iterator) called with an iterator not"
2333 " referring to this unodered container");
2334#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002335 for (const_iterator __p = __first; __first != __last; __p = __first)
2336 {
2337 ++__first;
2338 erase(__p);
2339 }
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00002340 __node_pointer __np = __last.__node_;
Howard Hinnant39213642013-07-23 22:01:58 +00002341#if _LIBCPP_DEBUG_LEVEL >= 2
2342 return iterator (__np, this);
2343#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002344 return iterator (__np);
Howard Hinnant39213642013-07-23 22:01:58 +00002345#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002346}
2347
2348template <class _Tp, class _Hash, class _Equal, class _Alloc>
2349template <class _Key>
2350typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2351__hash_table<_Tp, _Hash, _Equal, _Alloc>::__erase_unique(const _Key& __k)
2352{
2353 iterator __i = find(__k);
2354 if (__i == end())
2355 return 0;
2356 erase(__i);
2357 return 1;
2358}
2359
2360template <class _Tp, class _Hash, class _Equal, class _Alloc>
2361template <class _Key>
2362typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2363__hash_table<_Tp, _Hash, _Equal, _Alloc>::__erase_multi(const _Key& __k)
2364{
2365 size_type __r = 0;
2366 iterator __i = find(__k);
2367 if (__i != end())
2368 {
2369 iterator __e = end();
2370 do
2371 {
2372 erase(__i++);
2373 ++__r;
2374 } while (__i != __e && key_eq()(*__i, __k));
2375 }
2376 return __r;
2377}
2378
2379template <class _Tp, class _Hash, class _Equal, class _Alloc>
2380typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00002381__hash_table<_Tp, _Hash, _Equal, _Alloc>::remove(const_iterator __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002382{
2383 // current node
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00002384 __node_pointer __cn = __p.__node_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002385 size_type __bc = bucket_count();
Howard Hinnant7a445152012-07-06 17:31:14 +00002386 size_t __chash = __constrain_hash(__cn->__hash_, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002387 // find previous node
2388 __node_pointer __pn = __bucket_list_[__chash];
2389 for (; __pn->__next_ != __cn; __pn = __pn->__next_)
2390 ;
2391 // Fix up __bucket_list_
2392 // if __pn is not in same bucket (before begin is not in same bucket) &&
2393 // if __cn->__next_ is not in same bucket (nullptr is not in same bucket)
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00002394 if (__pn == static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()))
2395 || __constrain_hash(__pn->__hash_, __bc) != __chash)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002396 {
Howard Hinnant7a445152012-07-06 17:31:14 +00002397 if (__cn->__next_ == nullptr || __constrain_hash(__cn->__next_->__hash_, __bc) != __chash)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002398 __bucket_list_[__chash] = nullptr;
2399 }
2400 // if __cn->__next_ is not in same bucket (nullptr is in same bucket)
2401 if (__cn->__next_ != nullptr)
2402 {
Howard Hinnant7a445152012-07-06 17:31:14 +00002403 size_t __nhash = __constrain_hash(__cn->__next_->__hash_, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002404 if (__nhash != __chash)
2405 __bucket_list_[__nhash] = __pn;
2406 }
2407 // remove __cn
2408 __pn->__next_ = __cn->__next_;
2409 __cn->__next_ = nullptr;
2410 --size();
Howard Hinnant39213642013-07-23 22:01:58 +00002411#if _LIBCPP_DEBUG_LEVEL >= 2
2412 __c_node* __c = __get_db()->__find_c_and_lock(this);
2413 for (__i_node** __p = __c->end_; __p != __c->beg_; )
2414 {
2415 --__p;
2416 iterator* __i = static_cast<iterator*>((*__p)->__i_);
2417 if (__i->__node_ == __cn)
2418 {
2419 (*__p)->__c_ = nullptr;
2420 if (--__c->end_ != __p)
2421 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
2422 }
2423 }
2424 __get_db()->unlock();
2425#endif
Howard Hinnant99968442011-11-29 18:15:50 +00002426 return __node_holder(__cn, _Dp(__node_alloc(), true));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002427}
2428
2429template <class _Tp, class _Hash, class _Equal, class _Alloc>
2430template <class _Key>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00002431inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002432typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2433__hash_table<_Tp, _Hash, _Equal, _Alloc>::__count_unique(const _Key& __k) const
2434{
2435 return static_cast<size_type>(find(__k) != end());
2436}
2437
2438template <class _Tp, class _Hash, class _Equal, class _Alloc>
2439template <class _Key>
2440typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2441__hash_table<_Tp, _Hash, _Equal, _Alloc>::__count_multi(const _Key& __k) const
2442{
2443 size_type __r = 0;
2444 const_iterator __i = find(__k);
2445 if (__i != end())
2446 {
2447 const_iterator __e = end();
2448 do
2449 {
2450 ++__i;
2451 ++__r;
2452 } while (__i != __e && key_eq()(*__i, __k));
2453 }
2454 return __r;
2455}
2456
2457template <class _Tp, class _Hash, class _Equal, class _Alloc>
2458template <class _Key>
2459pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator,
2460 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator>
2461__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_unique(
2462 const _Key& __k)
2463{
2464 iterator __i = find(__k);
2465 iterator __j = __i;
2466 if (__i != end())
2467 ++__j;
2468 return pair<iterator, iterator>(__i, __j);
2469}
2470
2471template <class _Tp, class _Hash, class _Equal, class _Alloc>
2472template <class _Key>
2473pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator,
2474 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator>
2475__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_unique(
2476 const _Key& __k) const
2477{
2478 const_iterator __i = find(__k);
2479 const_iterator __j = __i;
2480 if (__i != end())
2481 ++__j;
2482 return pair<const_iterator, const_iterator>(__i, __j);
2483}
2484
2485template <class _Tp, class _Hash, class _Equal, class _Alloc>
2486template <class _Key>
2487pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator,
2488 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator>
2489__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_multi(
2490 const _Key& __k)
2491{
2492 iterator __i = find(__k);
2493 iterator __j = __i;
2494 if (__i != end())
2495 {
2496 iterator __e = end();
2497 do
2498 {
2499 ++__j;
2500 } while (__j != __e && key_eq()(*__j, __k));
2501 }
2502 return pair<iterator, iterator>(__i, __j);
2503}
2504
2505template <class _Tp, class _Hash, class _Equal, class _Alloc>
2506template <class _Key>
2507pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator,
2508 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator>
2509__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_multi(
2510 const _Key& __k) const
2511{
2512 const_iterator __i = find(__k);
2513 const_iterator __j = __i;
2514 if (__i != end())
2515 {
2516 const_iterator __e = end();
2517 do
2518 {
2519 ++__j;
2520 } while (__j != __e && key_eq()(*__j, __k));
2521 }
2522 return pair<const_iterator, const_iterator>(__i, __j);
2523}
2524
2525template <class _Tp, class _Hash, class _Equal, class _Alloc>
2526void
2527__hash_table<_Tp, _Hash, _Equal, _Alloc>::swap(__hash_table& __u)
Eric Fiselier692177d2015-07-18 20:40:46 +00002528#if _LIBCPP_STD_VER <= 11
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00002529 _NOEXCEPT_(
Marshall Clow7d914d12015-07-13 20:04:56 +00002530 __is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value
Marshall Clow7d914d12015-07-13 20:04:56 +00002531 && (!allocator_traits<__pointer_allocator>::propagate_on_container_swap::value
2532 || __is_nothrow_swappable<__pointer_allocator>::value)
2533 && (!__node_traits::propagate_on_container_swap::value
2534 || __is_nothrow_swappable<__node_allocator>::value)
Marshall Clow7d914d12015-07-13 20:04:56 +00002535 )
Eric Fiselier692177d2015-07-18 20:40:46 +00002536#else
2537 _NOEXCEPT_(__is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value)
2538#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002539{
2540 {
2541 __node_pointer_pointer __npp = __bucket_list_.release();
2542 __bucket_list_.reset(__u.__bucket_list_.release());
2543 __u.__bucket_list_.reset(__npp);
2544 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002545 _VSTD::swap(__bucket_list_.get_deleter().size(), __u.__bucket_list_.get_deleter().size());
Marshall Clow7d914d12015-07-13 20:04:56 +00002546 __swap_allocator(__bucket_list_.get_deleter().__alloc(),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002547 __u.__bucket_list_.get_deleter().__alloc());
Marshall Clow7d914d12015-07-13 20:04:56 +00002548 __swap_allocator(__node_alloc(), __u.__node_alloc());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002549 _VSTD::swap(__p1_.first().__next_, __u.__p1_.first().__next_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002550 __p2_.swap(__u.__p2_);
2551 __p3_.swap(__u.__p3_);
2552 if (size() > 0)
Howard Hinnant7a445152012-07-06 17:31:14 +00002553 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash_, bucket_count())] =
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00002554 static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002555 if (__u.size() > 0)
Howard Hinnant7a445152012-07-06 17:31:14 +00002556 __u.__bucket_list_[__constrain_hash(__u.__p1_.first().__next_->__hash_, __u.bucket_count())] =
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00002557 static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__u.__p1_.first()));
Howard Hinnant39213642013-07-23 22:01:58 +00002558#if _LIBCPP_DEBUG_LEVEL >= 2
2559 __get_db()->swap(this, &__u);
2560#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002561}
2562
2563template <class _Tp, class _Hash, class _Equal, class _Alloc>
2564typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2565__hash_table<_Tp, _Hash, _Equal, _Alloc>::bucket_size(size_type __n) const
2566{
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00002567 _LIBCPP_ASSERT(__n < bucket_count(),
2568 "unordered container::bucket_size(n) called with n >= bucket_count()");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002569 __node_const_pointer __np = __bucket_list_[__n];
2570 size_type __bc = bucket_count();
2571 size_type __r = 0;
2572 if (__np != nullptr)
2573 {
2574 for (__np = __np->__next_; __np != nullptr &&
Howard Hinnant7a445152012-07-06 17:31:14 +00002575 __constrain_hash(__np->__hash_, __bc) == __n;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002576 __np = __np->__next_, ++__r)
2577 ;
2578 }
2579 return __r;
2580}
2581
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00002582template <class _Tp, class _Hash, class _Equal, class _Alloc>
2583inline _LIBCPP_INLINE_VISIBILITY
2584void
2585swap(__hash_table<_Tp, _Hash, _Equal, _Alloc>& __x,
2586 __hash_table<_Tp, _Hash, _Equal, _Alloc>& __y)
2587 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
2588{
2589 __x.swap(__y);
2590}
2591
Howard Hinnant39213642013-07-23 22:01:58 +00002592#if _LIBCPP_DEBUG_LEVEL >= 2
2593
2594template <class _Tp, class _Hash, class _Equal, class _Alloc>
2595bool
2596__hash_table<_Tp, _Hash, _Equal, _Alloc>::__dereferenceable(const const_iterator* __i) const
2597{
2598 return __i->__node_ != nullptr;
2599}
2600
2601template <class _Tp, class _Hash, class _Equal, class _Alloc>
2602bool
2603__hash_table<_Tp, _Hash, _Equal, _Alloc>::__decrementable(const const_iterator*) const
2604{
2605 return false;
2606}
2607
2608template <class _Tp, class _Hash, class _Equal, class _Alloc>
2609bool
2610__hash_table<_Tp, _Hash, _Equal, _Alloc>::__addable(const const_iterator*, ptrdiff_t) const
2611{
2612 return false;
2613}
2614
2615template <class _Tp, class _Hash, class _Equal, class _Alloc>
2616bool
2617__hash_table<_Tp, _Hash, _Equal, _Alloc>::__subscriptable(const const_iterator*, ptrdiff_t) const
2618{
2619 return false;
2620}
2621
2622#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002623_LIBCPP_END_NAMESPACE_STD
2624
2625#endif // _LIBCPP__HASH_TABLE