blob: 6a0e6fea08aac0362d6051961b2b10fe4907a23c [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{
Eric Fiselierf6535882016-07-23 20:36:55 +000062 typedef typename pointer_traits<_NodePtr>::element_type __node_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000063 typedef __hash_node_base __first_node;
Eric Fiselierf6535882016-07-23 20:36:55 +000064 typedef typename __rebind_pointer<_NodePtr, __first_node>::type __node_base_pointer;
65 typedef _NodePtr __node_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000066
Eric Fiselierf6535882016-07-23 20:36:55 +000067#if defined(_LIBCPP_ABI_FIX_UNORDERED_NODE_POINTER_UB)
68 typedef __node_base_pointer __next_pointer;
69#else
70 typedef typename conditional<
71 is_pointer<__node_pointer>::value,
72 __node_base_pointer,
73 __node_pointer>::type __next_pointer;
74#endif
75
76 __next_pointer __next_;
77
78 _LIBCPP_INLINE_VISIBILITY
79 __next_pointer __ptr() _NOEXCEPT {
80 return static_cast<__next_pointer>(
81 pointer_traits<__node_base_pointer>::pointer_to(*this));
82 }
83
84 _LIBCPP_INLINE_VISIBILITY
85 __node_pointer __upcast() _NOEXCEPT {
86 return static_cast<__node_pointer>(
87 pointer_traits<__node_base_pointer>::pointer_to(*this));
88 }
89
90 _LIBCPP_INLINE_VISIBILITY
91 size_t __hash() const _NOEXCEPT {
92 return static_cast<__node_type const&>(*this).__hash_;
93 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000094
Howard Hinnant5f2f14c2011-06-04 18:54:24 +000095 _LIBCPP_INLINE_VISIBILITY __hash_node_base() _NOEXCEPT : __next_(nullptr) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000096};
97
98template <class _Tp, class _VoidPtr>
99struct __hash_node
100 : public __hash_node_base
101 <
Eric Fiselier5cf84e02015-12-30 21:52:00 +0000102 typename __rebind_pointer<_VoidPtr, __hash_node<_Tp, _VoidPtr> >::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000103 >
104{
Eric Fiselier774c7c52016-02-10 20:46:23 +0000105 typedef _Tp __node_value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000106
Eric Fiselierf6535882016-07-23 20:36:55 +0000107 size_t __hash_;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000108 __node_value_type __value_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000109};
110
Howard Hinnant7a445152012-07-06 17:31:14 +0000111inline _LIBCPP_INLINE_VISIBILITY
112bool
Eric Fiselier57947ca2015-02-02 21:31:48 +0000113__is_hash_power2(size_t __bc)
Howard Hinnant7a445152012-07-06 17:31:14 +0000114{
115 return __bc > 2 && !(__bc & (__bc - 1));
116}
117
118inline _LIBCPP_INLINE_VISIBILITY
119size_t
120__constrain_hash(size_t __h, size_t __bc)
121{
Eric Fiselier57663912016-07-11 22:02:02 +0000122 return !(__bc & (__bc - 1)) ? __h & (__bc - 1) :
123 (__h < __bc ? __h : __h % __bc);
Howard Hinnant7a445152012-07-06 17:31:14 +0000124}
125
126inline _LIBCPP_INLINE_VISIBILITY
127size_t
Eric Fiselier57947ca2015-02-02 21:31:48 +0000128__next_hash_pow2(size_t __n)
Howard Hinnant7a445152012-07-06 17:31:14 +0000129{
130 return size_t(1) << (std::numeric_limits<size_t>::digits - __clz(__n-1));
131}
132
Duncan P. N. Exon Smith798ec842016-03-17 20:45:20 +0000133
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000134template <class _Tp, class _Hash, class _Equal, class _Alloc> class __hash_table;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000135
136template <class _NodePtr> class _LIBCPP_TYPE_VIS_ONLY __hash_iterator;
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000137template <class _ConstNodePtr> class _LIBCPP_TYPE_VIS_ONLY __hash_const_iterator;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000138template <class _NodePtr> class _LIBCPP_TYPE_VIS_ONLY __hash_local_iterator;
139template <class _ConstNodePtr> class _LIBCPP_TYPE_VIS_ONLY __hash_const_local_iterator;
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000140template <class _HashIterator> class _LIBCPP_TYPE_VIS_ONLY __hash_map_iterator;
141template <class _HashIterator> class _LIBCPP_TYPE_VIS_ONLY __hash_map_const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000142
Eric Fiselier774c7c52016-02-10 20:46:23 +0000143template <class _Tp>
Eric Fiselier66e344f2016-02-20 07:59:16 +0000144struct __hash_key_value_types {
Eric Fiselier774c7c52016-02-10 20:46:23 +0000145 static_assert(!is_reference<_Tp>::value && !is_const<_Tp>::value, "");
146 typedef _Tp key_type;
147 typedef _Tp __node_value_type;
148 typedef _Tp __container_value_type;
149 static const bool __is_map = false;
Eric Fiselier2960ae22016-02-11 11:59:44 +0000150
151 _LIBCPP_INLINE_VISIBILITY
152 static key_type const& __get_key(_Tp const& __v) {
153 return __v;
154 }
155 _LIBCPP_INLINE_VISIBILITY
156 static __container_value_type const& __get_value(__node_value_type const& __v) {
157 return __v;
158 }
159 _LIBCPP_INLINE_VISIBILITY
160 static __container_value_type* __get_ptr(__node_value_type& __n) {
161 return _VSTD::addressof(__n);
162 }
163#ifndef _LIBCPP_CXX03_LANG
164 _LIBCPP_INLINE_VISIBILITY
165 static __container_value_type&& __move(__node_value_type& __v) {
166 return _VSTD::move(__v);
167 }
168#endif
Eric Fiselier774c7c52016-02-10 20:46:23 +0000169};
170
171template <class _Key, class _Tp>
Eric Fiselier66e344f2016-02-20 07:59:16 +0000172struct __hash_key_value_types<__hash_value_type<_Key, _Tp> > {
Eric Fiselier774c7c52016-02-10 20:46:23 +0000173 typedef _Key key_type;
174 typedef _Tp mapped_type;
175 typedef __hash_value_type<_Key, _Tp> __node_value_type;
176 typedef pair<const _Key, _Tp> __container_value_type;
Eric Fiselier2960ae22016-02-11 11:59:44 +0000177 typedef pair<_Key, _Tp> __nc_value_type;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000178 typedef __container_value_type __map_value_type;
179 static const bool __is_map = true;
Eric Fiselier2960ae22016-02-11 11:59:44 +0000180
181 _LIBCPP_INLINE_VISIBILITY
182 static key_type const& __get_key(__container_value_type const& __v) {
183 return __v.first;
184 }
185
186 template <class _Up>
187 _LIBCPP_INLINE_VISIBILITY
188 static typename enable_if<__is_same_uncvref<_Up, __node_value_type>::value,
189 __container_value_type const&>::type
190 __get_value(_Up& __t) {
191 return __t.__cc;
192 }
193
194 template <class _Up>
195 _LIBCPP_INLINE_VISIBILITY
196 static typename enable_if<__is_same_uncvref<_Up, __container_value_type>::value,
197 __container_value_type const&>::type
198 __get_value(_Up& __t) {
199 return __t;
200 }
201
202 _LIBCPP_INLINE_VISIBILITY
203 static __container_value_type* __get_ptr(__node_value_type& __n) {
204 return _VSTD::addressof(__n.__cc);
205 }
206#ifndef _LIBCPP_CXX03_LANG
207 _LIBCPP_INLINE_VISIBILITY
208 static __nc_value_type&& __move(__node_value_type& __v) {
209 return _VSTD::move(__v.__nc);
210 }
211#endif
212
Eric Fiselier774c7c52016-02-10 20:46:23 +0000213};
214
Eric Fiselier66e344f2016-02-20 07:59:16 +0000215template <class _Tp, class _AllocPtr, class _KVTypes = __hash_key_value_types<_Tp>,
Eric Fiselier774c7c52016-02-10 20:46:23 +0000216 bool = _KVTypes::__is_map>
Eric Fiselier66e344f2016-02-20 07:59:16 +0000217struct __hash_map_pointer_types {};
Eric Fiselier774c7c52016-02-10 20:46:23 +0000218
219template <class _Tp, class _AllocPtr, class _KVTypes>
Eric Fiselier66e344f2016-02-20 07:59:16 +0000220struct __hash_map_pointer_types<_Tp, _AllocPtr, _KVTypes, true> {
Eric Fiselier774c7c52016-02-10 20:46:23 +0000221 typedef typename _KVTypes::__map_value_type _Mv;
222 typedef typename __rebind_pointer<_AllocPtr, _Mv>::type
223 __map_value_type_pointer;
224 typedef typename __rebind_pointer<_AllocPtr, const _Mv>::type
225 __const_map_value_type_pointer;
226};
227
228template <class _NodePtr, class _NodeT = typename pointer_traits<_NodePtr>::element_type>
229struct __hash_node_types;
230
231template <class _NodePtr, class _Tp, class _VoidPtr>
232struct __hash_node_types<_NodePtr, __hash_node<_Tp, _VoidPtr> >
Eric Fiselier66e344f2016-02-20 07:59:16 +0000233 : public __hash_key_value_types<_Tp>, __hash_map_pointer_types<_Tp, _VoidPtr>
Eric Fiselier774c7c52016-02-10 20:46:23 +0000234
235{
Eric Fiselier66e344f2016-02-20 07:59:16 +0000236 typedef __hash_key_value_types<_Tp> __base;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000237
238public:
239 typedef ptrdiff_t difference_type;
240 typedef size_t size_type;
241
242 typedef typename __rebind_pointer<_NodePtr, void>::type __void_pointer;
243
244 typedef typename pointer_traits<_NodePtr>::element_type __node_type;
245 typedef _NodePtr __node_pointer;
246
247 typedef __hash_node_base<__node_pointer> __node_base_type;
248 typedef typename __rebind_pointer<_NodePtr, __node_base_type>::type
249 __node_base_pointer;
250
Eric Fiselierf6535882016-07-23 20:36:55 +0000251 typedef typename __node_base_type::__next_pointer __next_pointer;
252
Eric Fiselier774c7c52016-02-10 20:46:23 +0000253 typedef _Tp __node_value_type;
254 typedef typename __rebind_pointer<_VoidPtr, __node_value_type>::type
255 __node_value_type_pointer;
256 typedef typename __rebind_pointer<_VoidPtr, const __node_value_type>::type
257 __const_node_value_type_pointer;
Eric Fiselierf6535882016-07-23 20:36:55 +0000258
Eric Fiselier774c7c52016-02-10 20:46:23 +0000259private:
260 static_assert(!is_const<__node_type>::value,
261 "_NodePtr should never be a pointer to const");
262 static_assert((is_same<typename pointer_traits<_VoidPtr>::element_type, void>::value),
263 "_VoidPtr does not point to unqualified void type");
264 static_assert((is_same<typename __rebind_pointer<_VoidPtr, __node_type>::type,
265 _NodePtr>::value), "_VoidPtr does not rebind to _NodePtr.");
266};
267
Eric Fiselier774c7c52016-02-10 20:46:23 +0000268template <class _HashIterator>
269struct __hash_node_types_from_iterator;
270template <class _NodePtr>
271struct __hash_node_types_from_iterator<__hash_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
272template <class _NodePtr>
273struct __hash_node_types_from_iterator<__hash_const_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
274template <class _NodePtr>
275struct __hash_node_types_from_iterator<__hash_local_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
276template <class _NodePtr>
277struct __hash_node_types_from_iterator<__hash_const_local_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
278
279
280template <class _NodeValueTp, class _VoidPtr>
281struct __make_hash_node_types {
282 typedef __hash_node<_NodeValueTp, _VoidPtr> _NodeTp;
283 typedef typename __rebind_pointer<_VoidPtr, _NodeTp>::type _NodePtr;
284 typedef __hash_node_types<_NodePtr> type;
285};
286
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000287template <class _NodePtr>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000288class _LIBCPP_TYPE_VIS_ONLY __hash_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000289{
Eric Fiselier774c7c52016-02-10 20:46:23 +0000290 typedef __hash_node_types<_NodePtr> _NodeTypes;
Eric Fiselierf6535882016-07-23 20:36:55 +0000291 typedef _NodePtr __node_pointer;
292 typedef typename _NodeTypes::__next_pointer __next_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000293
Eric Fiselierf6535882016-07-23 20:36:55 +0000294 __next_pointer __node_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000295
296public:
Eric Fiselier774c7c52016-02-10 20:46:23 +0000297 typedef forward_iterator_tag iterator_category;
298 typedef typename _NodeTypes::__node_value_type value_type;
299 typedef typename _NodeTypes::difference_type difference_type;
300 typedef value_type& reference;
301 typedef typename _NodeTypes::__node_value_type_pointer pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000302
Eric Fiselierf6535882016-07-23 20:36:55 +0000303 _LIBCPP_INLINE_VISIBILITY __hash_iterator() _NOEXCEPT : __node_(nullptr) {
304 _LIBCPP_DEBUG_MODE(__get_db()->__insert_i(this));
Howard Hinnant39213642013-07-23 22:01:58 +0000305 }
306
307#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant99acc502010-09-21 17:32:39 +0000308 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000309 __hash_iterator(const __hash_iterator& __i)
310 : __node_(__i.__node_)
311 {
312 __get_db()->__iterator_copy(this, &__i);
313 }
314
Howard Hinnant99acc502010-09-21 17:32:39 +0000315 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000316 ~__hash_iterator()
317 {
318 __get_db()->__erase_i(this);
319 }
320
321 _LIBCPP_INLINE_VISIBILITY
322 __hash_iterator& operator=(const __hash_iterator& __i)
323 {
324 if (this != &__i)
325 {
326 __get_db()->__iterator_copy(this, &__i);
327 __node_ = __i.__node_;
328 }
329 return *this;
330 }
Howard Hinnant39213642013-07-23 22:01:58 +0000331#endif // _LIBCPP_DEBUG_LEVEL >= 2
332
333 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierf6535882016-07-23 20:36:55 +0000334 reference operator*() const {
335 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
336 "Attempted to dereference a non-dereferenceable unordered container iterator");
337 return __node_->__upcast()->__value_;
338 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000339
Howard Hinnant99acc502010-09-21 17:32:39 +0000340 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierf6535882016-07-23 20:36:55 +0000341 pointer operator->() const {
342 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
343 "Attempted to dereference a non-dereferenceable unordered container iterator");
344 return pointer_traits<pointer>::pointer_to(__node_->__upcast()->__value_);
345 }
346
347 _LIBCPP_INLINE_VISIBILITY
348 __hash_iterator& operator++() {
349 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
Howard Hinnant39213642013-07-23 22:01:58 +0000350 "Attempted to increment non-incrementable unordered container iterator");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000351 __node_ = __node_->__next_;
352 return *this;
353 }
354
Howard Hinnant99acc502010-09-21 17:32:39 +0000355 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000356 __hash_iterator operator++(int)
357 {
358 __hash_iterator __t(*this);
359 ++(*this);
360 return __t;
361 }
362
Howard Hinnant99acc502010-09-21 17:32:39 +0000363 friend _LIBCPP_INLINE_VISIBILITY
364 bool operator==(const __hash_iterator& __x, const __hash_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58 +0000365 {
Howard Hinnant39213642013-07-23 22:01:58 +0000366 return __x.__node_ == __y.__node_;
367 }
Howard Hinnant99acc502010-09-21 17:32:39 +0000368 friend _LIBCPP_INLINE_VISIBILITY
369 bool operator!=(const __hash_iterator& __x, const __hash_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58 +0000370 {return !(__x == __y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000371
372private:
Howard Hinnant39213642013-07-23 22:01:58 +0000373#if _LIBCPP_DEBUG_LEVEL >= 2
374 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierf6535882016-07-23 20:36:55 +0000375 __hash_iterator(__next_pointer __node, const void* __c) _NOEXCEPT
Howard Hinnant39213642013-07-23 22:01:58 +0000376 : __node_(__node)
377 {
378 __get_db()->__insert_ic(this, __c);
379 }
380#else
Howard Hinnant99acc502010-09-21 17:32:39 +0000381 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierf6535882016-07-23 20:36:55 +0000382 __hash_iterator(__next_pointer __node) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000383 : __node_(__node)
384 {}
Howard Hinnant39213642013-07-23 22:01:58 +0000385#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000386 template <class, class, class, class> friend class __hash_table;
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000387 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_const_iterator;
388 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_map_iterator;
389 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_map;
390 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_multimap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000391};
392
Eric Fiselier774c7c52016-02-10 20:46:23 +0000393template <class _NodePtr>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000394class _LIBCPP_TYPE_VIS_ONLY __hash_const_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000395{
Eric Fiselier774c7c52016-02-10 20:46:23 +0000396 static_assert(!is_const<typename pointer_traits<_NodePtr>::element_type>::value, "");
397 typedef __hash_node_types<_NodePtr> _NodeTypes;
Eric Fiselierf6535882016-07-23 20:36:55 +0000398 typedef _NodePtr __node_pointer;
399 typedef typename _NodeTypes::__next_pointer __next_pointer;
Eric Fiselier0493d022016-02-18 00:20:34 +0000400
Eric Fiselierf6535882016-07-23 20:36:55 +0000401 __next_pointer __node_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000402
403public:
Eric Fiselier0493d022016-02-18 00:20:34 +0000404 typedef __hash_iterator<_NodePtr> __non_const_iterator;
405
Eric Fiselier774c7c52016-02-10 20:46:23 +0000406 typedef forward_iterator_tag iterator_category;
407 typedef typename _NodeTypes::__node_value_type value_type;
408 typedef typename _NodeTypes::difference_type difference_type;
409 typedef const value_type& reference;
410 typedef typename _NodeTypes::__const_node_value_type_pointer pointer;
411
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000412
Eric Fiselierf6535882016-07-23 20:36:55 +0000413 _LIBCPP_INLINE_VISIBILITY __hash_const_iterator() _NOEXCEPT : __node_(nullptr) {
414 _LIBCPP_DEBUG_MODE(__get_db()->__insert_i(this));
Howard Hinnant39213642013-07-23 22:01:58 +0000415 }
Eric Fiselierf6535882016-07-23 20:36:55 +0000416
Howard Hinnant99acc502010-09-21 17:32:39 +0000417 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000418 __hash_const_iterator(const __non_const_iterator& __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000419 : __node_(__x.__node_)
Howard Hinnant39213642013-07-23 22:01:58 +0000420 {
Eric Fiselierf6535882016-07-23 20:36:55 +0000421 _LIBCPP_DEBUG_MODE(__get_db()->__iterator_copy(this, &__x));
Howard Hinnant39213642013-07-23 22:01:58 +0000422 }
423
424#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant99acc502010-09-21 17:32:39 +0000425 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000426 __hash_const_iterator(const __hash_const_iterator& __i)
427 : __node_(__i.__node_)
428 {
429 __get_db()->__iterator_copy(this, &__i);
430 }
431
Howard Hinnant99acc502010-09-21 17:32:39 +0000432 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000433 ~__hash_const_iterator()
434 {
435 __get_db()->__erase_i(this);
436 }
437
438 _LIBCPP_INLINE_VISIBILITY
439 __hash_const_iterator& operator=(const __hash_const_iterator& __i)
440 {
441 if (this != &__i)
442 {
443 __get_db()->__iterator_copy(this, &__i);
444 __node_ = __i.__node_;
445 }
446 return *this;
447 }
Howard Hinnant39213642013-07-23 22:01:58 +0000448#endif // _LIBCPP_DEBUG_LEVEL >= 2
449
450 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierf6535882016-07-23 20:36:55 +0000451 reference operator*() const {
452 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
Howard Hinnant39213642013-07-23 22:01:58 +0000453 "Attempted to dereference a non-dereferenceable unordered container const_iterator");
Eric Fiselierf6535882016-07-23 20:36:55 +0000454 return __node_->__upcast()->__value_;
455 }
Howard Hinnant39213642013-07-23 22:01:58 +0000456 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierf6535882016-07-23 20:36:55 +0000457 pointer operator->() const {
458 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
Howard Hinnant39213642013-07-23 22:01:58 +0000459 "Attempted to dereference a non-dereferenceable unordered container const_iterator");
Eric Fiselierf6535882016-07-23 20:36:55 +0000460 return pointer_traits<pointer>::pointer_to(__node_->__upcast()->__value_);
461 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000462
Howard Hinnant99acc502010-09-21 17:32:39 +0000463 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierf6535882016-07-23 20:36:55 +0000464 __hash_const_iterator& operator++() {
465 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
466 "Attempted to increment non-incrementable unordered container const_iterator");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000467 __node_ = __node_->__next_;
468 return *this;
469 }
470
Howard Hinnant99acc502010-09-21 17:32:39 +0000471 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000472 __hash_const_iterator operator++(int)
473 {
474 __hash_const_iterator __t(*this);
475 ++(*this);
476 return __t;
477 }
478
Howard Hinnant99acc502010-09-21 17:32:39 +0000479 friend _LIBCPP_INLINE_VISIBILITY
480 bool operator==(const __hash_const_iterator& __x, const __hash_const_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58 +0000481 {
Howard Hinnant39213642013-07-23 22:01:58 +0000482 return __x.__node_ == __y.__node_;
483 }
Howard Hinnant99acc502010-09-21 17:32:39 +0000484 friend _LIBCPP_INLINE_VISIBILITY
485 bool operator!=(const __hash_const_iterator& __x, const __hash_const_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58 +0000486 {return !(__x == __y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000487
488private:
Howard Hinnant39213642013-07-23 22:01:58 +0000489#if _LIBCPP_DEBUG_LEVEL >= 2
490 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierf6535882016-07-23 20:36:55 +0000491 __hash_const_iterator(__next_pointer __node, const void* __c) _NOEXCEPT
Howard Hinnant39213642013-07-23 22:01:58 +0000492 : __node_(__node)
493 {
494 __get_db()->__insert_ic(this, __c);
495 }
496#else
Howard Hinnant99acc502010-09-21 17:32:39 +0000497 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierf6535882016-07-23 20:36:55 +0000498 __hash_const_iterator(__next_pointer __node) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000499 : __node_(__node)
500 {}
Howard Hinnant39213642013-07-23 22:01:58 +0000501#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000502 template <class, class, class, class> friend class __hash_table;
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000503 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_map_const_iterator;
504 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_map;
505 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_multimap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000506};
507
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000508template <class _NodePtr>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000509class _LIBCPP_TYPE_VIS_ONLY __hash_local_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000510{
Eric Fiselier774c7c52016-02-10 20:46:23 +0000511 typedef __hash_node_types<_NodePtr> _NodeTypes;
Eric Fiselierf6535882016-07-23 20:36:55 +0000512 typedef _NodePtr __node_pointer;
513 typedef typename _NodeTypes::__next_pointer __next_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000514
Eric Fiselierf6535882016-07-23 20:36:55 +0000515 __next_pointer __node_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000516 size_t __bucket_;
517 size_t __bucket_count_;
518
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000519public:
520 typedef forward_iterator_tag iterator_category;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000521 typedef typename _NodeTypes::__node_value_type value_type;
522 typedef typename _NodeTypes::difference_type difference_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000523 typedef value_type& reference;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000524 typedef typename _NodeTypes::__node_value_type_pointer pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000525
Eric Fiselierf6535882016-07-23 20:36:55 +0000526 _LIBCPP_INLINE_VISIBILITY __hash_local_iterator() _NOEXCEPT : __node_(nullptr) {
527 _LIBCPP_DEBUG_MODE(__get_db()->__insert_i(this));
Howard Hinnant39213642013-07-23 22:01:58 +0000528 }
529
530#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant99acc502010-09-21 17:32:39 +0000531 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000532 __hash_local_iterator(const __hash_local_iterator& __i)
533 : __node_(__i.__node_),
534 __bucket_(__i.__bucket_),
535 __bucket_count_(__i.__bucket_count_)
536 {
537 __get_db()->__iterator_copy(this, &__i);
538 }
539
Howard Hinnant99acc502010-09-21 17:32:39 +0000540 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000541 ~__hash_local_iterator()
542 {
543 __get_db()->__erase_i(this);
544 }
545
546 _LIBCPP_INLINE_VISIBILITY
547 __hash_local_iterator& operator=(const __hash_local_iterator& __i)
548 {
549 if (this != &__i)
550 {
551 __get_db()->__iterator_copy(this, &__i);
552 __node_ = __i.__node_;
553 __bucket_ = __i.__bucket_;
554 __bucket_count_ = __i.__bucket_count_;
555 }
556 return *this;
557 }
Howard Hinnant39213642013-07-23 22:01:58 +0000558#endif // _LIBCPP_DEBUG_LEVEL >= 2
559
560 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierf6535882016-07-23 20:36:55 +0000561 reference operator*() const {
562 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
Howard Hinnant39213642013-07-23 22:01:58 +0000563 "Attempted to dereference a non-dereferenceable unordered container local_iterator");
Eric Fiselierf6535882016-07-23 20:36:55 +0000564 return __node_->__upcast()->__value_;
565 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000566
Howard Hinnant99acc502010-09-21 17:32:39 +0000567 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierf6535882016-07-23 20:36:55 +0000568 pointer operator->() const {
569 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
570 "Attempted to dereference a non-dereferenceable unordered container local_iterator");
571 return pointer_traits<pointer>::pointer_to(__node_->__upcast()->__value_);
572 }
573
574 _LIBCPP_INLINE_VISIBILITY
575 __hash_local_iterator& operator++() {
576 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
Howard Hinnant39213642013-07-23 22:01:58 +0000577 "Attempted to increment non-incrementable unordered container local_iterator");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000578 __node_ = __node_->__next_;
Eric Fiselierf6535882016-07-23 20:36:55 +0000579 if (__node_ != nullptr && __constrain_hash(__node_->__hash(), __bucket_count_) != __bucket_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000580 __node_ = nullptr;
581 return *this;
582 }
583
Howard Hinnant99acc502010-09-21 17:32:39 +0000584 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000585 __hash_local_iterator operator++(int)
586 {
587 __hash_local_iterator __t(*this);
588 ++(*this);
589 return __t;
590 }
591
Howard Hinnant99acc502010-09-21 17:32:39 +0000592 friend _LIBCPP_INLINE_VISIBILITY
593 bool operator==(const __hash_local_iterator& __x, const __hash_local_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58 +0000594 {
Howard Hinnant39213642013-07-23 22:01:58 +0000595 return __x.__node_ == __y.__node_;
596 }
Howard Hinnant99acc502010-09-21 17:32:39 +0000597 friend _LIBCPP_INLINE_VISIBILITY
598 bool operator!=(const __hash_local_iterator& __x, const __hash_local_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58 +0000599 {return !(__x == __y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000600
601private:
Howard Hinnant39213642013-07-23 22:01:58 +0000602#if _LIBCPP_DEBUG_LEVEL >= 2
603 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierf6535882016-07-23 20:36:55 +0000604 __hash_local_iterator(__next_pointer __node, size_t __bucket,
Howard Hinnant39213642013-07-23 22:01:58 +0000605 size_t __bucket_count, const void* __c) _NOEXCEPT
606 : __node_(__node),
607 __bucket_(__bucket),
608 __bucket_count_(__bucket_count)
609 {
610 __get_db()->__insert_ic(this, __c);
611 if (__node_ != nullptr)
612 __node_ = __node_->__next_;
613 }
614#else
Howard Hinnant99acc502010-09-21 17:32:39 +0000615 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierf6535882016-07-23 20:36:55 +0000616 __hash_local_iterator(__next_pointer __node, size_t __bucket,
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000617 size_t __bucket_count) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000618 : __node_(__node),
619 __bucket_(__bucket),
620 __bucket_count_(__bucket_count)
621 {
622 if (__node_ != nullptr)
623 __node_ = __node_->__next_;
624 }
Howard Hinnant39213642013-07-23 22:01:58 +0000625#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000626 template <class, class, class, class> friend class __hash_table;
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000627 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_const_local_iterator;
628 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_map_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000629};
630
631template <class _ConstNodePtr>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000632class _LIBCPP_TYPE_VIS_ONLY __hash_const_local_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000633{
Eric Fiselier774c7c52016-02-10 20:46:23 +0000634 typedef __hash_node_types<_ConstNodePtr> _NodeTypes;
Eric Fiselierf6535882016-07-23 20:36:55 +0000635 typedef _ConstNodePtr __node_pointer;
636 typedef typename _NodeTypes::__next_pointer __next_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000637
Eric Fiselierf6535882016-07-23 20:36:55 +0000638 __next_pointer __node_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000639 size_t __bucket_;
640 size_t __bucket_count_;
641
642 typedef pointer_traits<__node_pointer> __pointer_traits;
643 typedef typename __pointer_traits::element_type __node;
644 typedef typename remove_const<__node>::type __non_const_node;
Eric Fiselier5cf84e02015-12-30 21:52:00 +0000645 typedef typename __rebind_pointer<__node_pointer, __non_const_node>::type
646 __non_const_node_pointer;
Eric Fiselier0493d022016-02-18 00:20:34 +0000647public:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000648 typedef __hash_local_iterator<__non_const_node_pointer>
649 __non_const_iterator;
Eric Fiselier0493d022016-02-18 00:20:34 +0000650
Eric Fiselier774c7c52016-02-10 20:46:23 +0000651 typedef forward_iterator_tag iterator_category;
652 typedef typename _NodeTypes::__node_value_type value_type;
653 typedef typename _NodeTypes::difference_type difference_type;
654 typedef const value_type& reference;
655 typedef typename _NodeTypes::__const_node_value_type_pointer pointer;
Eric Fiselier5cf84e02015-12-30 21:52:00 +0000656
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000657
Eric Fiselierf6535882016-07-23 20:36:55 +0000658 _LIBCPP_INLINE_VISIBILITY __hash_const_local_iterator() _NOEXCEPT : __node_(nullptr) {
659 _LIBCPP_DEBUG_MODE(__get_db()->__insert_i(this));
Howard Hinnant39213642013-07-23 22:01:58 +0000660 }
661
Howard Hinnant99acc502010-09-21 17:32:39 +0000662 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000663 __hash_const_local_iterator(const __non_const_iterator& __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000664 : __node_(__x.__node_),
665 __bucket_(__x.__bucket_),
666 __bucket_count_(__x.__bucket_count_)
Howard Hinnant39213642013-07-23 22:01:58 +0000667 {
Eric Fiselierf6535882016-07-23 20:36:55 +0000668 _LIBCPP_DEBUG_MODE(__get_db()->__iterator_copy(this, &__x));
Howard Hinnant39213642013-07-23 22:01:58 +0000669 }
670
671#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant99acc502010-09-21 17:32:39 +0000672 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000673 __hash_const_local_iterator(const __hash_const_local_iterator& __i)
674 : __node_(__i.__node_),
675 __bucket_(__i.__bucket_),
676 __bucket_count_(__i.__bucket_count_)
677 {
678 __get_db()->__iterator_copy(this, &__i);
679 }
680
Howard Hinnant99acc502010-09-21 17:32:39 +0000681 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000682 ~__hash_const_local_iterator()
683 {
684 __get_db()->__erase_i(this);
685 }
686
687 _LIBCPP_INLINE_VISIBILITY
688 __hash_const_local_iterator& operator=(const __hash_const_local_iterator& __i)
689 {
690 if (this != &__i)
691 {
692 __get_db()->__iterator_copy(this, &__i);
693 __node_ = __i.__node_;
694 __bucket_ = __i.__bucket_;
695 __bucket_count_ = __i.__bucket_count_;
696 }
697 return *this;
698 }
Howard Hinnant39213642013-07-23 22:01:58 +0000699#endif // _LIBCPP_DEBUG_LEVEL >= 2
700
701 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierf6535882016-07-23 20:36:55 +0000702 reference operator*() const {
703 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
Howard Hinnant39213642013-07-23 22:01:58 +0000704 "Attempted to dereference a non-dereferenceable unordered container const_local_iterator");
Eric Fiselierf6535882016-07-23 20:36:55 +0000705 return __node_->__upcast()->__value_;
706 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000707
Howard Hinnant99acc502010-09-21 17:32:39 +0000708 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierf6535882016-07-23 20:36:55 +0000709 pointer operator->() const {
710 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
711 "Attempted to dereference a non-dereferenceable unordered container const_local_iterator");
712 return pointer_traits<pointer>::pointer_to(__node_->__upcast()->__value_);
713 }
714
715 _LIBCPP_INLINE_VISIBILITY
716 __hash_const_local_iterator& operator++() {
717 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
Howard Hinnant39213642013-07-23 22:01:58 +0000718 "Attempted to increment non-incrementable unordered container const_local_iterator");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000719 __node_ = __node_->__next_;
Eric Fiselierf6535882016-07-23 20:36:55 +0000720 if (__node_ != nullptr && __constrain_hash(__node_->__hash(), __bucket_count_) != __bucket_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000721 __node_ = nullptr;
722 return *this;
723 }
724
Howard Hinnant99acc502010-09-21 17:32:39 +0000725 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000726 __hash_const_local_iterator operator++(int)
727 {
728 __hash_const_local_iterator __t(*this);
729 ++(*this);
730 return __t;
731 }
732
Howard Hinnant99acc502010-09-21 17:32:39 +0000733 friend _LIBCPP_INLINE_VISIBILITY
734 bool operator==(const __hash_const_local_iterator& __x, const __hash_const_local_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58 +0000735 {
Howard Hinnant39213642013-07-23 22:01:58 +0000736 return __x.__node_ == __y.__node_;
737 }
Howard Hinnant99acc502010-09-21 17:32:39 +0000738 friend _LIBCPP_INLINE_VISIBILITY
739 bool operator!=(const __hash_const_local_iterator& __x, const __hash_const_local_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58 +0000740 {return !(__x == __y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000741
742private:
Howard Hinnant39213642013-07-23 22:01:58 +0000743#if _LIBCPP_DEBUG_LEVEL >= 2
744 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierf6535882016-07-23 20:36:55 +0000745 __hash_const_local_iterator(__next_pointer __node, size_t __bucket,
Howard Hinnant39213642013-07-23 22:01:58 +0000746 size_t __bucket_count, const void* __c) _NOEXCEPT
747 : __node_(__node),
748 __bucket_(__bucket),
749 __bucket_count_(__bucket_count)
750 {
751 __get_db()->__insert_ic(this, __c);
752 if (__node_ != nullptr)
753 __node_ = __node_->__next_;
754 }
755#else
Howard Hinnant99acc502010-09-21 17:32:39 +0000756 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierf6535882016-07-23 20:36:55 +0000757 __hash_const_local_iterator(__next_pointer __node, size_t __bucket,
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000758 size_t __bucket_count) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000759 : __node_(__node),
760 __bucket_(__bucket),
761 __bucket_count_(__bucket_count)
762 {
763 if (__node_ != nullptr)
764 __node_ = __node_->__next_;
765 }
Howard Hinnant39213642013-07-23 22:01:58 +0000766#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000767 template <class, class, class, class> friend class __hash_table;
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000768 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_map_const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000769};
770
771template <class _Alloc>
772class __bucket_list_deallocator
773{
774 typedef _Alloc allocator_type;
775 typedef allocator_traits<allocator_type> __alloc_traits;
776 typedef typename __alloc_traits::size_type size_type;
777
778 __compressed_pair<size_type, allocator_type> __data_;
779public:
780 typedef typename __alloc_traits::pointer pointer;
781
Howard Hinnant99acc502010-09-21 17:32:39 +0000782 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000783 __bucket_list_deallocator()
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000784 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000785 : __data_(0) {}
Howard Hinnant99acc502010-09-21 17:32:39 +0000786
787 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000788 __bucket_list_deallocator(const allocator_type& __a, size_type __size)
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000789 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000790 : __data_(__size, __a) {}
791
Howard Hinnant73d21a42010-09-04 23:28:19 +0000792#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000793
Howard Hinnant99acc502010-09-21 17:32:39 +0000794 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000795 __bucket_list_deallocator(__bucket_list_deallocator&& __x)
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000796 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000797 : __data_(_VSTD::move(__x.__data_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000798 {
799 __x.size() = 0;
800 }
801
Howard Hinnant73d21a42010-09-04 23:28:19 +0000802#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000803
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000804 _LIBCPP_INLINE_VISIBILITY
805 size_type& size() _NOEXCEPT {return __data_.first();}
806 _LIBCPP_INLINE_VISIBILITY
807 size_type size() const _NOEXCEPT {return __data_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000808
Howard Hinnant99acc502010-09-21 17:32:39 +0000809 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000810 allocator_type& __alloc() _NOEXCEPT {return __data_.second();}
811 _LIBCPP_INLINE_VISIBILITY
812 const allocator_type& __alloc() const _NOEXCEPT {return __data_.second();}
813
814 _LIBCPP_INLINE_VISIBILITY
815 void operator()(pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000816 {
817 __alloc_traits::deallocate(__alloc(), __p, size());
818 }
819};
820
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000821template <class _Alloc> class __hash_map_node_destructor;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000822
823template <class _Alloc>
824class __hash_node_destructor
825{
826 typedef _Alloc allocator_type;
827 typedef allocator_traits<allocator_type> __alloc_traits;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000828
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000829public:
830 typedef typename __alloc_traits::pointer pointer;
831private:
Eric Fiselier2960ae22016-02-11 11:59:44 +0000832 typedef __hash_node_types<pointer> _NodeTypes;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000833
834 allocator_type& __na_;
835
836 __hash_node_destructor& operator=(const __hash_node_destructor&);
837
838public:
839 bool __value_constructed;
840
Howard Hinnant99acc502010-09-21 17:32:39 +0000841 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant199d0ae2011-07-31 17:04:30 +0000842 explicit __hash_node_destructor(allocator_type& __na,
843 bool __constructed = false) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000844 : __na_(__na),
Howard Hinnant199d0ae2011-07-31 17:04:30 +0000845 __value_constructed(__constructed)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000846 {}
847
Howard Hinnant99acc502010-09-21 17:32:39 +0000848 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000849 void operator()(pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000850 {
851 if (__value_constructed)
Eric Fiselier2960ae22016-02-11 11:59:44 +0000852 __alloc_traits::destroy(__na_, _NodeTypes::__get_ptr(__p->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000853 if (__p)
854 __alloc_traits::deallocate(__na_, __p, 1);
855 }
856
857 template <class> friend class __hash_map_node_destructor;
858};
859
860template <class _Tp, class _Hash, class _Equal, class _Alloc>
861class __hash_table
862{
863public:
864 typedef _Tp value_type;
865 typedef _Hash hasher;
866 typedef _Equal key_equal;
867 typedef _Alloc allocator_type;
868
869private:
870 typedef allocator_traits<allocator_type> __alloc_traits;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000871 typedef typename
872 __make_hash_node_types<value_type, typename __alloc_traits::void_pointer>::type
873 _NodeTypes;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000874public:
Eric Fiselier2960ae22016-02-11 11:59:44 +0000875
876 typedef typename _NodeTypes::__node_value_type __node_value_type;
877 typedef typename _NodeTypes::__container_value_type __container_value_type;
Duncan P. N. Exon Smith798ec842016-03-17 20:45:20 +0000878 typedef typename _NodeTypes::key_type key_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000879 typedef value_type& reference;
880 typedef const value_type& const_reference;
881 typedef typename __alloc_traits::pointer pointer;
882 typedef typename __alloc_traits::const_pointer const_pointer;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000883#ifndef _LIBCPP_ABI_FIX_UNORDERED_CONTAINER_SIZE_TYPE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000884 typedef typename __alloc_traits::size_type size_type;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000885#else
886 typedef typename _NodeTypes::size_type size_type;
887#endif
888 typedef typename _NodeTypes::difference_type difference_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000889public:
890 // Create __node
Eric Fiselier774c7c52016-02-10 20:46:23 +0000891
892 typedef typename _NodeTypes::__node_type __node;
Marshall Clow66302c62015-04-07 05:21:38 +0000893 typedef typename __rebind_alloc_helper<__alloc_traits, __node>::type __node_allocator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000894 typedef allocator_traits<__node_allocator> __node_traits;
Eric Fiselier9e9f42e2016-02-11 15:22:37 +0000895 typedef typename _NodeTypes::__void_pointer __void_pointer;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000896 typedef typename _NodeTypes::__node_pointer __node_pointer;
897 typedef typename _NodeTypes::__node_pointer __node_const_pointer;
898 typedef typename _NodeTypes::__node_base_type __first_node;
899 typedef typename _NodeTypes::__node_base_pointer __node_base_pointer;
Eric Fiselierf6535882016-07-23 20:36:55 +0000900 typedef typename _NodeTypes::__next_pointer __next_pointer;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000901
902private:
903 // check for sane allocator pointer rebinding semantics. Rebinding the
904 // allocator for a new pointer type should be exactly the same as rebinding
905 // the pointer using 'pointer_traits'.
906 static_assert((is_same<__node_pointer, typename __node_traits::pointer>::value),
907 "Allocator does not rebind pointers in a sane manner.");
908 typedef typename __rebind_alloc_helper<__node_traits, __first_node>::type
909 __node_base_allocator;
910 typedef allocator_traits<__node_base_allocator> __node_base_traits;
911 static_assert((is_same<__node_base_pointer, typename __node_base_traits::pointer>::value),
912 "Allocator does not rebind pointers in a sane manner.");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000913
914private:
915
Eric Fiselierf6535882016-07-23 20:36:55 +0000916 typedef typename __rebind_alloc_helper<__node_traits, __next_pointer>::type __pointer_allocator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000917 typedef __bucket_list_deallocator<__pointer_allocator> __bucket_list_deleter;
Eric Fiselierf6535882016-07-23 20:36:55 +0000918 typedef unique_ptr<__next_pointer[], __bucket_list_deleter> __bucket_list;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000919 typedef allocator_traits<__pointer_allocator> __pointer_alloc_traits;
Eric Fiselierf6535882016-07-23 20:36:55 +0000920 typedef typename __bucket_list_deleter::pointer __node_pointer_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000921
922 // --- Member data begin ---
Eric Fiselier774c7c52016-02-10 20:46:23 +0000923 __bucket_list __bucket_list_;
924 __compressed_pair<__first_node, __node_allocator> __p1_;
925 __compressed_pair<size_type, hasher> __p2_;
926 __compressed_pair<float, key_equal> __p3_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000927 // --- Member data end ---
928
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000929 _LIBCPP_INLINE_VISIBILITY
930 size_type& size() _NOEXCEPT {return __p2_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000931public:
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000932 _LIBCPP_INLINE_VISIBILITY
933 size_type size() const _NOEXCEPT {return __p2_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000934
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000935 _LIBCPP_INLINE_VISIBILITY
936 hasher& hash_function() _NOEXCEPT {return __p2_.second();}
937 _LIBCPP_INLINE_VISIBILITY
938 const hasher& hash_function() const _NOEXCEPT {return __p2_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000939
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000940 _LIBCPP_INLINE_VISIBILITY
941 float& max_load_factor() _NOEXCEPT {return __p3_.first();}
942 _LIBCPP_INLINE_VISIBILITY
943 float max_load_factor() const _NOEXCEPT {return __p3_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000944
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000945 _LIBCPP_INLINE_VISIBILITY
946 key_equal& key_eq() _NOEXCEPT {return __p3_.second();}
947 _LIBCPP_INLINE_VISIBILITY
948 const key_equal& key_eq() const _NOEXCEPT {return __p3_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000949
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000950 _LIBCPP_INLINE_VISIBILITY
951 __node_allocator& __node_alloc() _NOEXCEPT {return __p1_.second();}
952 _LIBCPP_INLINE_VISIBILITY
953 const __node_allocator& __node_alloc() const _NOEXCEPT
954 {return __p1_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000955
956public:
957 typedef __hash_iterator<__node_pointer> iterator;
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +0000958 typedef __hash_const_iterator<__node_pointer> const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000959 typedef __hash_local_iterator<__node_pointer> local_iterator;
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +0000960 typedef __hash_const_local_iterator<__node_pointer> const_local_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000961
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000962 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000963 __hash_table()
964 _NOEXCEPT_(
965 is_nothrow_default_constructible<__bucket_list>::value &&
966 is_nothrow_default_constructible<__first_node>::value &&
967 is_nothrow_default_constructible<__node_allocator>::value &&
968 is_nothrow_default_constructible<hasher>::value &&
969 is_nothrow_default_constructible<key_equal>::value);
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000970 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000971 __hash_table(const hasher& __hf, const key_equal& __eql);
972 __hash_table(const hasher& __hf, const key_equal& __eql,
973 const allocator_type& __a);
974 explicit __hash_table(const allocator_type& __a);
975 __hash_table(const __hash_table& __u);
976 __hash_table(const __hash_table& __u, const allocator_type& __a);
Eric Fiselier2960ae22016-02-11 11:59:44 +0000977#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000978 __hash_table(__hash_table&& __u)
979 _NOEXCEPT_(
980 is_nothrow_move_constructible<__bucket_list>::value &&
981 is_nothrow_move_constructible<__first_node>::value &&
982 is_nothrow_move_constructible<__node_allocator>::value &&
983 is_nothrow_move_constructible<hasher>::value &&
984 is_nothrow_move_constructible<key_equal>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000985 __hash_table(__hash_table&& __u, const allocator_type& __a);
Eric Fiselier2960ae22016-02-11 11:59:44 +0000986#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000987 ~__hash_table();
988
989 __hash_table& operator=(const __hash_table& __u);
Eric Fiselier2960ae22016-02-11 11:59:44 +0000990#ifndef _LIBCPP_CXX03_LANG
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000991 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000992 __hash_table& operator=(__hash_table&& __u)
993 _NOEXCEPT_(
994 __node_traits::propagate_on_container_move_assignment::value &&
995 is_nothrow_move_assignable<__node_allocator>::value &&
996 is_nothrow_move_assignable<hasher>::value &&
997 is_nothrow_move_assignable<key_equal>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000998#endif
999 template <class _InputIterator>
1000 void __assign_unique(_InputIterator __first, _InputIterator __last);
1001 template <class _InputIterator>
1002 void __assign_multi(_InputIterator __first, _InputIterator __last);
1003
Howard Hinnant99acc502010-09-21 17:32:39 +00001004 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001005 size_type max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001006 {
1007 return allocator_traits<__pointer_allocator>::max_size(
1008 __bucket_list_.get_deleter().__alloc());
1009 }
1010
1011 pair<iterator, bool> __node_insert_unique(__node_pointer __nd);
1012 iterator __node_insert_multi(__node_pointer __nd);
1013 iterator __node_insert_multi(const_iterator __p,
1014 __node_pointer __nd);
1015
Eric Fiselier2960ae22016-02-11 11:59:44 +00001016#ifndef _LIBCPP_CXX03_LANG
1017 template <class _Key, class ..._Args>
Duncan P. N. Exon Smith798ec842016-03-17 20:45:20 +00001018 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2960ae22016-02-11 11:59:44 +00001019 pair<iterator, bool> __emplace_unique_key_args(_Key const& __k, _Args&&... __args);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001020
Eric Fiselier2960ae22016-02-11 11:59:44 +00001021 template <class... _Args>
Duncan P. N. Exon Smith798ec842016-03-17 20:45:20 +00001022 _LIBCPP_INLINE_VISIBILITY
1023 pair<iterator, bool> __emplace_unique_impl(_Args&&... __args);
1024
1025 template <class _Pp>
1026 _LIBCPP_INLINE_VISIBILITY
1027 pair<iterator, bool> __emplace_unique(_Pp&& __x) {
1028 return __emplace_unique_extract_key(_VSTD::forward<_Pp>(__x),
1029 __can_extract_key<_Pp, key_type>());
1030 }
Eric Fiselierdc414cd2016-04-16 00:23:12 +00001031
1032 template <class _First, class _Second>
1033 _LIBCPP_INLINE_VISIBILITY
1034 typename enable_if<
1035 __can_extract_map_key<_First, key_type, __container_value_type>::value,
1036 pair<iterator, bool>
1037 >::type __emplace_unique(_First&& __f, _Second&& __s) {
1038 return __emplace_unique_key_args(__f, _VSTD::forward<_First>(__f),
1039 _VSTD::forward<_Second>(__s));
1040 }
1041
Eric Fiselier2960ae22016-02-11 11:59:44 +00001042 template <class... _Args>
Duncan P. N. Exon Smith798ec842016-03-17 20:45:20 +00001043 _LIBCPP_INLINE_VISIBILITY
1044 pair<iterator, bool> __emplace_unique(_Args&&... __args) {
1045 return __emplace_unique_impl(_VSTD::forward<_Args>(__args)...);
1046 }
1047
1048 template <class _Pp>
1049 _LIBCPP_INLINE_VISIBILITY
1050 pair<iterator, bool>
1051 __emplace_unique_extract_key(_Pp&& __x, __extract_key_fail_tag) {
1052 return __emplace_unique_impl(_VSTD::forward<_Pp>(__x));
1053 }
1054 template <class _Pp>
1055 _LIBCPP_INLINE_VISIBILITY
1056 pair<iterator, bool>
1057 __emplace_unique_extract_key(_Pp&& __x, __extract_key_self_tag) {
1058 return __emplace_unique_key_args(__x, _VSTD::forward<_Pp>(__x));
1059 }
1060 template <class _Pp>
1061 _LIBCPP_INLINE_VISIBILITY
1062 pair<iterator, bool>
1063 __emplace_unique_extract_key(_Pp&& __x, __extract_key_first_tag) {
1064 return __emplace_unique_key_args(__x.first, _VSTD::forward<_Pp>(__x));
1065 }
1066
1067 template <class... _Args>
1068 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2960ae22016-02-11 11:59:44 +00001069 iterator __emplace_multi(_Args&&... __args);
1070 template <class... _Args>
Duncan P. N. Exon Smith798ec842016-03-17 20:45:20 +00001071 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2960ae22016-02-11 11:59:44 +00001072 iterator __emplace_hint_multi(const_iterator __p, _Args&&... __args);
1073
1074
Eric Fiselierfdae69a2015-06-13 07:18:32 +00001075 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2960ae22016-02-11 11:59:44 +00001076 pair<iterator, bool>
1077 __insert_unique(__container_value_type&& __x) {
1078 return __emplace_unique_key_args(_NodeTypes::__get_key(__x), _VSTD::move(__x));
1079 }
1080
1081 template <class _Pp, class = typename enable_if<
1082 !__is_same_uncvref<_Pp, __container_value_type>::value
1083 >::type>
Eric Fiselierfdae69a2015-06-13 07:18:32 +00001084 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2960ae22016-02-11 11:59:44 +00001085 pair<iterator, bool> __insert_unique(_Pp&& __x) {
1086 return __emplace_unique(_VSTD::forward<_Pp>(__x));
1087 }
1088
1089 template <class _Pp>
1090 _LIBCPP_INLINE_VISIBILITY
1091 iterator __insert_multi(_Pp&& __x) {
1092 return __emplace_multi(_VSTD::forward<_Pp>(__x));
1093 }
1094
1095 template <class _Pp>
1096 _LIBCPP_INLINE_VISIBILITY
1097 iterator __insert_multi(const_iterator __p, _Pp&& __x) {
1098 return __emplace_hint_multi(__p, _VSTD::forward<_Pp>(__x));
1099 }
1100
1101#else // !defined(_LIBCPP_CXX03_LANG)
1102 template <class _Key, class _Args>
1103 pair<iterator, bool> __emplace_unique_key_args(_Key const&, _Args& __args);
1104
1105 iterator __insert_multi(const __container_value_type& __x);
1106 iterator __insert_multi(const_iterator __p, const __container_value_type& __x);
Eric Fiselierfdae69a2015-06-13 07:18:32 +00001107#endif
1108
Eric Fiselier2960ae22016-02-11 11:59:44 +00001109 _LIBCPP_INLINE_VISIBILITY
1110 pair<iterator, bool> __insert_unique(const __container_value_type& __x) {
1111 return __emplace_unique_key_args(_NodeTypes::__get_key(__x), __x);
1112 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001113
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001114 void clear() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001115 void rehash(size_type __n);
Howard Hinnant99acc502010-09-21 17:32:39 +00001116 _LIBCPP_INLINE_VISIBILITY void reserve(size_type __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001117 {rehash(static_cast<size_type>(ceil(__n / max_load_factor())));}
Howard Hinnant99acc502010-09-21 17:32:39 +00001118
1119 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001120 size_type bucket_count() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001121 {
1122 return __bucket_list_.get_deleter().size();
1123 }
1124
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001125 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001126 iterator begin() _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001127 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001128 iterator end() _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001129 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001130 const_iterator begin() const _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001131 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001132 const_iterator end() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001133
1134 template <class _Key>
Howard Hinnant99acc502010-09-21 17:32:39 +00001135 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001136 size_type bucket(const _Key& __k) const
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00001137 {
1138 _LIBCPP_ASSERT(bucket_count() > 0,
1139 "unordered container::bucket(key) called when bucket_count() == 0");
1140 return __constrain_hash(hash_function()(__k), bucket_count());
1141 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001142
1143 template <class _Key>
1144 iterator find(const _Key& __x);
1145 template <class _Key>
1146 const_iterator find(const _Key& __x) const;
1147
Howard Hinnant99968442011-11-29 18:15:50 +00001148 typedef __hash_node_destructor<__node_allocator> _Dp;
1149 typedef unique_ptr<__node, _Dp> __node_holder;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001150
1151 iterator erase(const_iterator __p);
1152 iterator erase(const_iterator __first, const_iterator __last);
1153 template <class _Key>
1154 size_type __erase_unique(const _Key& __k);
1155 template <class _Key>
1156 size_type __erase_multi(const _Key& __k);
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001157 __node_holder remove(const_iterator __p) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001158
1159 template <class _Key>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001160 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001161 size_type __count_unique(const _Key& __k) const;
1162 template <class _Key>
1163 size_type __count_multi(const _Key& __k) const;
1164
1165 template <class _Key>
1166 pair<iterator, iterator>
1167 __equal_range_unique(const _Key& __k);
1168 template <class _Key>
1169 pair<const_iterator, const_iterator>
1170 __equal_range_unique(const _Key& __k) const;
1171
1172 template <class _Key>
1173 pair<iterator, iterator>
1174 __equal_range_multi(const _Key& __k);
1175 template <class _Key>
1176 pair<const_iterator, const_iterator>
1177 __equal_range_multi(const _Key& __k) const;
1178
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001179 void swap(__hash_table& __u)
Eric Fiselier692177d2015-07-18 20:40:46 +00001180#if _LIBCPP_STD_VER <= 11
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001181 _NOEXCEPT_(
Marshall Clow7d914d12015-07-13 20:04:56 +00001182 __is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value
Marshall Clow7d914d12015-07-13 20:04:56 +00001183 && (!allocator_traits<__pointer_allocator>::propagate_on_container_swap::value
1184 || __is_nothrow_swappable<__pointer_allocator>::value)
1185 && (!__node_traits::propagate_on_container_swap::value
1186 || __is_nothrow_swappable<__node_allocator>::value)
Marshall Clow7d914d12015-07-13 20:04:56 +00001187 );
Eric Fiselier692177d2015-07-18 20:40:46 +00001188#else
1189 _NOEXCEPT_(__is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value);
1190#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001191
Howard Hinnant99acc502010-09-21 17:32:39 +00001192 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001193 size_type max_bucket_count() const _NOEXCEPT
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001194 {return __pointer_alloc_traits::max_size(__bucket_list_.get_deleter().__alloc());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001195 size_type bucket_size(size_type __n) const;
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001196 _LIBCPP_INLINE_VISIBILITY float load_factor() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001197 {
1198 size_type __bc = bucket_count();
1199 return __bc != 0 ? (float)size() / __bc : 0.f;
1200 }
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001201 _LIBCPP_INLINE_VISIBILITY void max_load_factor(float __mlf) _NOEXCEPT
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00001202 {
1203 _LIBCPP_ASSERT(__mlf > 0,
1204 "unordered container::max_load_factor(lf) called with lf <= 0");
1205 max_load_factor() = _VSTD::max(__mlf, load_factor());
1206 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001207
Howard Hinnant39213642013-07-23 22:01:58 +00001208 _LIBCPP_INLINE_VISIBILITY
1209 local_iterator
1210 begin(size_type __n)
1211 {
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00001212 _LIBCPP_ASSERT(__n < bucket_count(),
1213 "unordered container::begin(n) called with n >= bucket_count()");
Howard Hinnant39213642013-07-23 22:01:58 +00001214#if _LIBCPP_DEBUG_LEVEL >= 2
1215 return local_iterator(__bucket_list_[__n], __n, bucket_count(), this);
1216#else
1217 return local_iterator(__bucket_list_[__n], __n, bucket_count());
1218#endif
1219 }
1220
1221 _LIBCPP_INLINE_VISIBILITY
1222 local_iterator
1223 end(size_type __n)
1224 {
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00001225 _LIBCPP_ASSERT(__n < bucket_count(),
1226 "unordered container::end(n) called with n >= bucket_count()");
Howard Hinnant39213642013-07-23 22:01:58 +00001227#if _LIBCPP_DEBUG_LEVEL >= 2
1228 return local_iterator(nullptr, __n, bucket_count(), this);
1229#else
1230 return local_iterator(nullptr, __n, bucket_count());
1231#endif
1232 }
1233
1234 _LIBCPP_INLINE_VISIBILITY
1235 const_local_iterator
1236 cbegin(size_type __n) const
1237 {
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00001238 _LIBCPP_ASSERT(__n < bucket_count(),
1239 "unordered container::cbegin(n) called with n >= bucket_count()");
Howard Hinnant39213642013-07-23 22:01:58 +00001240#if _LIBCPP_DEBUG_LEVEL >= 2
1241 return const_local_iterator(__bucket_list_[__n], __n, bucket_count(), this);
1242#else
1243 return const_local_iterator(__bucket_list_[__n], __n, bucket_count());
1244#endif
1245 }
1246
1247 _LIBCPP_INLINE_VISIBILITY
1248 const_local_iterator
1249 cend(size_type __n) const
1250 {
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00001251 _LIBCPP_ASSERT(__n < bucket_count(),
1252 "unordered container::cend(n) called with n >= bucket_count()");
Howard Hinnant39213642013-07-23 22:01:58 +00001253#if _LIBCPP_DEBUG_LEVEL >= 2
1254 return const_local_iterator(nullptr, __n, bucket_count(), this);
1255#else
1256 return const_local_iterator(nullptr, __n, bucket_count());
1257#endif
1258 }
1259
1260#if _LIBCPP_DEBUG_LEVEL >= 2
1261
1262 bool __dereferenceable(const const_iterator* __i) const;
1263 bool __decrementable(const const_iterator* __i) const;
1264 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1265 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1266
1267#endif // _LIBCPP_DEBUG_LEVEL >= 2
1268
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001269private:
1270 void __rehash(size_type __n);
1271
Eric Fiselier2960ae22016-02-11 11:59:44 +00001272#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001273 template <class ..._Args>
Eric Fiselier2960ae22016-02-11 11:59:44 +00001274 __node_holder __construct_node(_Args&& ...__args);
1275
1276 template <class _First, class ..._Rest>
1277 __node_holder __construct_node_hash(size_t __hash, _First&& __f, _Rest&&... __rest);
1278#else // _LIBCPP_CXX03_LANG
1279 __node_holder __construct_node(const __container_value_type& __v);
1280 __node_holder __construct_node_hash(size_t __hash, const __container_value_type& __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001281#endif
Eric Fiselier2960ae22016-02-11 11:59:44 +00001282
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001283
Howard Hinnant99acc502010-09-21 17:32:39 +00001284 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001285 void __copy_assign_alloc(const __hash_table& __u)
1286 {__copy_assign_alloc(__u, integral_constant<bool,
1287 __node_traits::propagate_on_container_copy_assignment::value>());}
1288 void __copy_assign_alloc(const __hash_table& __u, true_type);
Howard Hinnant99acc502010-09-21 17:32:39 +00001289 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00001290 void __copy_assign_alloc(const __hash_table&, false_type) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001291
Eric Fiselier2960ae22016-02-11 11:59:44 +00001292#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001293 void __move_assign(__hash_table& __u, false_type);
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001294 void __move_assign(__hash_table& __u, true_type)
1295 _NOEXCEPT_(
1296 is_nothrow_move_assignable<__node_allocator>::value &&
1297 is_nothrow_move_assignable<hasher>::value &&
1298 is_nothrow_move_assignable<key_equal>::value);
1299 _LIBCPP_INLINE_VISIBILITY
1300 void __move_assign_alloc(__hash_table& __u)
1301 _NOEXCEPT_(
1302 !__node_traits::propagate_on_container_move_assignment::value ||
1303 (is_nothrow_move_assignable<__pointer_allocator>::value &&
1304 is_nothrow_move_assignable<__node_allocator>::value))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001305 {__move_assign_alloc(__u, integral_constant<bool,
1306 __node_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnant99acc502010-09-21 17:32:39 +00001307 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001308 void __move_assign_alloc(__hash_table& __u, true_type)
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001309 _NOEXCEPT_(
1310 is_nothrow_move_assignable<__pointer_allocator>::value &&
1311 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001312 {
1313 __bucket_list_.get_deleter().__alloc() =
Howard Hinnant0949eed2011-06-30 21:18:19 +00001314 _VSTD::move(__u.__bucket_list_.get_deleter().__alloc());
1315 __node_alloc() = _VSTD::move(__u.__node_alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001316 }
Howard Hinnant99acc502010-09-21 17:32:39 +00001317 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001318 void __move_assign_alloc(__hash_table&, false_type) _NOEXCEPT {}
Eric Fiselier2960ae22016-02-11 11:59:44 +00001319#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001320
Eric Fiselierf6535882016-07-23 20:36:55 +00001321 void __deallocate(__next_pointer __np) _NOEXCEPT;
1322 __next_pointer __detach() _NOEXCEPT;
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001323
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001324 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_map;
1325 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_multimap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001326};
1327
1328template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001329inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001330__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table()
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001331 _NOEXCEPT_(
1332 is_nothrow_default_constructible<__bucket_list>::value &&
1333 is_nothrow_default_constructible<__first_node>::value &&
Eric Fiselierc8f54c22015-12-16 00:53:04 +00001334 is_nothrow_default_constructible<__node_allocator>::value &&
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001335 is_nothrow_default_constructible<hasher>::value &&
1336 is_nothrow_default_constructible<key_equal>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001337 : __p2_(0),
1338 __p3_(1.0f)
1339{
1340}
1341
1342template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001343inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001344__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const hasher& __hf,
1345 const key_equal& __eql)
1346 : __bucket_list_(nullptr, __bucket_list_deleter()),
1347 __p1_(),
1348 __p2_(0, __hf),
1349 __p3_(1.0f, __eql)
1350{
1351}
1352
1353template <class _Tp, class _Hash, class _Equal, class _Alloc>
1354__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const hasher& __hf,
1355 const key_equal& __eql,
1356 const allocator_type& __a)
1357 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1358 __p1_(__node_allocator(__a)),
1359 __p2_(0, __hf),
1360 __p3_(1.0f, __eql)
1361{
1362}
1363
1364template <class _Tp, class _Hash, class _Equal, class _Alloc>
1365__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const allocator_type& __a)
1366 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1367 __p1_(__node_allocator(__a)),
1368 __p2_(0),
1369 __p3_(1.0f)
1370{
1371}
1372
1373template <class _Tp, class _Hash, class _Equal, class _Alloc>
1374__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const __hash_table& __u)
1375 : __bucket_list_(nullptr,
1376 __bucket_list_deleter(allocator_traits<__pointer_allocator>::
1377 select_on_container_copy_construction(
1378 __u.__bucket_list_.get_deleter().__alloc()), 0)),
1379 __p1_(allocator_traits<__node_allocator>::
1380 select_on_container_copy_construction(__u.__node_alloc())),
1381 __p2_(0, __u.hash_function()),
1382 __p3_(__u.__p3_)
1383{
1384}
1385
1386template <class _Tp, class _Hash, class _Equal, class _Alloc>
1387__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const __hash_table& __u,
1388 const allocator_type& __a)
1389 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1390 __p1_(__node_allocator(__a)),
1391 __p2_(0, __u.hash_function()),
1392 __p3_(__u.__p3_)
1393{
1394}
1395
Eric Fiselier2960ae22016-02-11 11:59:44 +00001396#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001397
1398template <class _Tp, class _Hash, class _Equal, class _Alloc>
1399__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(__hash_table&& __u)
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001400 _NOEXCEPT_(
1401 is_nothrow_move_constructible<__bucket_list>::value &&
1402 is_nothrow_move_constructible<__first_node>::value &&
Eric Fiselierc8f54c22015-12-16 00:53:04 +00001403 is_nothrow_move_constructible<__node_allocator>::value &&
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001404 is_nothrow_move_constructible<hasher>::value &&
1405 is_nothrow_move_constructible<key_equal>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001406 : __bucket_list_(_VSTD::move(__u.__bucket_list_)),
1407 __p1_(_VSTD::move(__u.__p1_)),
1408 __p2_(_VSTD::move(__u.__p2_)),
1409 __p3_(_VSTD::move(__u.__p3_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001410{
1411 if (size() > 0)
1412 {
Eric Fiselierf6535882016-07-23 20:36:55 +00001413 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash(), bucket_count())] =
1414 __p1_.first().__ptr();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001415 __u.__p1_.first().__next_ = nullptr;
1416 __u.size() = 0;
1417 }
1418}
1419
1420template <class _Tp, class _Hash, class _Equal, class _Alloc>
1421__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(__hash_table&& __u,
1422 const allocator_type& __a)
1423 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1424 __p1_(__node_allocator(__a)),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001425 __p2_(0, _VSTD::move(__u.hash_function())),
1426 __p3_(_VSTD::move(__u.__p3_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001427{
1428 if (__a == allocator_type(__u.__node_alloc()))
1429 {
1430 __bucket_list_.reset(__u.__bucket_list_.release());
1431 __bucket_list_.get_deleter().size() = __u.__bucket_list_.get_deleter().size();
1432 __u.__bucket_list_.get_deleter().size() = 0;
1433 if (__u.size() > 0)
1434 {
1435 __p1_.first().__next_ = __u.__p1_.first().__next_;
1436 __u.__p1_.first().__next_ = nullptr;
Eric Fiselierf6535882016-07-23 20:36:55 +00001437 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash(), bucket_count())] =
1438 __p1_.first().__ptr();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001439 size() = __u.size();
1440 __u.size() = 0;
1441 }
1442 }
1443}
1444
Eric Fiselier2960ae22016-02-11 11:59:44 +00001445#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001446
1447template <class _Tp, class _Hash, class _Equal, class _Alloc>
1448__hash_table<_Tp, _Hash, _Equal, _Alloc>::~__hash_table()
1449{
Marshall Clow1a933122016-06-30 22:05:45 +00001450 static_assert((is_copy_constructible<key_equal>::value),
1451 "Predicate must be copy-constructible.");
1452 static_assert((is_copy_constructible<hasher>::value),
1453 "Hasher must be copy-constructible.");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001454 __deallocate(__p1_.first().__next_);
Howard Hinnant39213642013-07-23 22:01:58 +00001455#if _LIBCPP_DEBUG_LEVEL >= 2
1456 __get_db()->__erase_c(this);
1457#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001458}
1459
1460template <class _Tp, class _Hash, class _Equal, class _Alloc>
1461void
1462__hash_table<_Tp, _Hash, _Equal, _Alloc>::__copy_assign_alloc(
1463 const __hash_table& __u, true_type)
1464{
1465 if (__node_alloc() != __u.__node_alloc())
1466 {
1467 clear();
1468 __bucket_list_.reset();
1469 __bucket_list_.get_deleter().size() = 0;
1470 }
1471 __bucket_list_.get_deleter().__alloc() = __u.__bucket_list_.get_deleter().__alloc();
1472 __node_alloc() = __u.__node_alloc();
1473}
1474
1475template <class _Tp, class _Hash, class _Equal, class _Alloc>
1476__hash_table<_Tp, _Hash, _Equal, _Alloc>&
1477__hash_table<_Tp, _Hash, _Equal, _Alloc>::operator=(const __hash_table& __u)
1478{
1479 if (this != &__u)
1480 {
1481 __copy_assign_alloc(__u);
1482 hash_function() = __u.hash_function();
1483 key_eq() = __u.key_eq();
1484 max_load_factor() = __u.max_load_factor();
1485 __assign_multi(__u.begin(), __u.end());
1486 }
1487 return *this;
1488}
1489
1490template <class _Tp, class _Hash, class _Equal, class _Alloc>
1491void
Eric Fiselierf6535882016-07-23 20:36:55 +00001492__hash_table<_Tp, _Hash, _Equal, _Alloc>::__deallocate(__next_pointer __np)
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001493 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001494{
1495 __node_allocator& __na = __node_alloc();
1496 while (__np != nullptr)
1497 {
Eric Fiselierf6535882016-07-23 20:36:55 +00001498 __next_pointer __next = __np->__next_;
Howard Hinnant39213642013-07-23 22:01:58 +00001499#if _LIBCPP_DEBUG_LEVEL >= 2
1500 __c_node* __c = __get_db()->__find_c_and_lock(this);
1501 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1502 {
1503 --__p;
1504 iterator* __i = static_cast<iterator*>((*__p)->__i_);
1505 if (__i->__node_ == __np)
1506 {
1507 (*__p)->__c_ = nullptr;
1508 if (--__c->end_ != __p)
1509 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1510 }
1511 }
1512 __get_db()->unlock();
1513#endif
Eric Fiselierf6535882016-07-23 20:36:55 +00001514 __node_pointer __real_np = __np->__upcast();
1515 __node_traits::destroy(__na, _NodeTypes::__get_ptr(__real_np->__value_));
1516 __node_traits::deallocate(__na, __real_np, 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001517 __np = __next;
1518 }
1519}
1520
1521template <class _Tp, class _Hash, class _Equal, class _Alloc>
Eric Fiselierf6535882016-07-23 20:36:55 +00001522typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__next_pointer
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001523__hash_table<_Tp, _Hash, _Equal, _Alloc>::__detach() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001524{
1525 size_type __bc = bucket_count();
1526 for (size_type __i = 0; __i < __bc; ++__i)
1527 __bucket_list_[__i] = nullptr;
1528 size() = 0;
Eric Fiselierf6535882016-07-23 20:36:55 +00001529 __next_pointer __cache = __p1_.first().__next_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001530 __p1_.first().__next_ = nullptr;
1531 return __cache;
1532}
1533
Eric Fiselier2960ae22016-02-11 11:59:44 +00001534#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001535
1536template <class _Tp, class _Hash, class _Equal, class _Alloc>
1537void
1538__hash_table<_Tp, _Hash, _Equal, _Alloc>::__move_assign(
1539 __hash_table& __u, true_type)
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001540 _NOEXCEPT_(
1541 is_nothrow_move_assignable<__node_allocator>::value &&
1542 is_nothrow_move_assignable<hasher>::value &&
1543 is_nothrow_move_assignable<key_equal>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001544{
1545 clear();
1546 __bucket_list_.reset(__u.__bucket_list_.release());
1547 __bucket_list_.get_deleter().size() = __u.__bucket_list_.get_deleter().size();
1548 __u.__bucket_list_.get_deleter().size() = 0;
1549 __move_assign_alloc(__u);
1550 size() = __u.size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001551 hash_function() = _VSTD::move(__u.hash_function());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001552 max_load_factor() = __u.max_load_factor();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001553 key_eq() = _VSTD::move(__u.key_eq());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001554 __p1_.first().__next_ = __u.__p1_.first().__next_;
1555 if (size() > 0)
1556 {
Eric Fiselierf6535882016-07-23 20:36:55 +00001557 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash(), bucket_count())] =
1558 __p1_.first().__ptr();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001559 __u.__p1_.first().__next_ = nullptr;
1560 __u.size() = 0;
1561 }
Howard Hinnant39213642013-07-23 22:01:58 +00001562#if _LIBCPP_DEBUG_LEVEL >= 2
1563 __get_db()->swap(this, &__u);
1564#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001565}
1566
1567template <class _Tp, class _Hash, class _Equal, class _Alloc>
1568void
1569__hash_table<_Tp, _Hash, _Equal, _Alloc>::__move_assign(
1570 __hash_table& __u, false_type)
1571{
1572 if (__node_alloc() == __u.__node_alloc())
1573 __move_assign(__u, true_type());
1574 else
1575 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001576 hash_function() = _VSTD::move(__u.hash_function());
1577 key_eq() = _VSTD::move(__u.key_eq());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001578 max_load_factor() = __u.max_load_factor();
1579 if (bucket_count() != 0)
1580 {
Eric Fiselierf6535882016-07-23 20:36:55 +00001581 __next_pointer __cache = __detach();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001582#ifndef _LIBCPP_NO_EXCEPTIONS
1583 try
1584 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001585#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001586 const_iterator __i = __u.begin();
1587 while (__cache != nullptr && __u.size() != 0)
1588 {
Eric Fiselierf6535882016-07-23 20:36:55 +00001589 __cache->__upcast()->__value_ =
1590 _VSTD::move(__u.remove(__i++)->__value_);
1591 __next_pointer __next = __cache->__next_;
1592 __node_insert_multi(__cache->__upcast());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001593 __cache = __next;
1594 }
1595#ifndef _LIBCPP_NO_EXCEPTIONS
1596 }
1597 catch (...)
1598 {
1599 __deallocate(__cache);
1600 throw;
1601 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001602#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001603 __deallocate(__cache);
1604 }
1605 const_iterator __i = __u.begin();
1606 while (__u.size() != 0)
1607 {
Eric Fiselier2960ae22016-02-11 11:59:44 +00001608 __node_holder __h = __construct_node(_NodeTypes::__move(__u.remove(__i++)->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001609 __node_insert_multi(__h.get());
1610 __h.release();
1611 }
1612 }
1613}
1614
1615template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001616inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001617__hash_table<_Tp, _Hash, _Equal, _Alloc>&
1618__hash_table<_Tp, _Hash, _Equal, _Alloc>::operator=(__hash_table&& __u)
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001619 _NOEXCEPT_(
1620 __node_traits::propagate_on_container_move_assignment::value &&
1621 is_nothrow_move_assignable<__node_allocator>::value &&
1622 is_nothrow_move_assignable<hasher>::value &&
1623 is_nothrow_move_assignable<key_equal>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001624{
1625 __move_assign(__u, integral_constant<bool,
1626 __node_traits::propagate_on_container_move_assignment::value>());
1627 return *this;
1628}
1629
Eric Fiselier2960ae22016-02-11 11:59:44 +00001630#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001631
1632template <class _Tp, class _Hash, class _Equal, class _Alloc>
1633template <class _InputIterator>
1634void
1635__hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_unique(_InputIterator __first,
1636 _InputIterator __last)
1637{
Eric Fiselier2960ae22016-02-11 11:59:44 +00001638 typedef iterator_traits<_InputIterator> _ITraits;
1639 typedef typename _ITraits::value_type _ItValueType;
1640 static_assert((is_same<_ItValueType, __container_value_type>::value),
1641 "__assign_unique may only be called with the containers value type");
1642
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001643 if (bucket_count() != 0)
1644 {
Eric Fiselierf6535882016-07-23 20:36:55 +00001645 __next_pointer __cache = __detach();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001646#ifndef _LIBCPP_NO_EXCEPTIONS
1647 try
1648 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001649#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001650 for (; __cache != nullptr && __first != __last; ++__first)
1651 {
Eric Fiselierf6535882016-07-23 20:36:55 +00001652 __cache->__upcast()->__value_ = *__first;
1653 __next_pointer __next = __cache->__next_;
1654 __node_insert_unique(__cache->__upcast());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001655 __cache = __next;
1656 }
1657#ifndef _LIBCPP_NO_EXCEPTIONS
1658 }
1659 catch (...)
1660 {
1661 __deallocate(__cache);
1662 throw;
1663 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001664#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001665 __deallocate(__cache);
1666 }
1667 for (; __first != __last; ++__first)
1668 __insert_unique(*__first);
1669}
1670
1671template <class _Tp, class _Hash, class _Equal, class _Alloc>
1672template <class _InputIterator>
1673void
1674__hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_multi(_InputIterator __first,
1675 _InputIterator __last)
1676{
Eric Fiselier2960ae22016-02-11 11:59:44 +00001677 typedef iterator_traits<_InputIterator> _ITraits;
1678 typedef typename _ITraits::value_type _ItValueType;
1679 static_assert((is_same<_ItValueType, __container_value_type>::value ||
1680 is_same<_ItValueType, __node_value_type>::value),
1681 "__assign_multi may only be called with the containers value type"
1682 " or the nodes value type");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001683 if (bucket_count() != 0)
1684 {
Eric Fiselierf6535882016-07-23 20:36:55 +00001685 __next_pointer __cache = __detach();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001686#ifndef _LIBCPP_NO_EXCEPTIONS
1687 try
1688 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001689#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001690 for (; __cache != nullptr && __first != __last; ++__first)
1691 {
Eric Fiselierf6535882016-07-23 20:36:55 +00001692 __cache->__upcast()->__value_ = *__first;
1693 __next_pointer __next = __cache->__next_;
1694 __node_insert_multi(__cache->__upcast());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001695 __cache = __next;
1696 }
1697#ifndef _LIBCPP_NO_EXCEPTIONS
1698 }
1699 catch (...)
1700 {
1701 __deallocate(__cache);
1702 throw;
1703 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001704#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001705 __deallocate(__cache);
1706 }
1707 for (; __first != __last; ++__first)
Eric Fiselier2960ae22016-02-11 11:59:44 +00001708 __insert_multi(_NodeTypes::__get_value(*__first));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001709}
1710
1711template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001712inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001713typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001714__hash_table<_Tp, _Hash, _Equal, _Alloc>::begin() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001715{
Howard Hinnant39213642013-07-23 22:01:58 +00001716#if _LIBCPP_DEBUG_LEVEL >= 2
1717 return iterator(__p1_.first().__next_, this);
1718#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001719 return iterator(__p1_.first().__next_);
Howard Hinnant39213642013-07-23 22:01:58 +00001720#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001721}
1722
1723template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001724inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001725typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001726__hash_table<_Tp, _Hash, _Equal, _Alloc>::end() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001727{
Howard Hinnant39213642013-07-23 22:01:58 +00001728#if _LIBCPP_DEBUG_LEVEL >= 2
1729 return iterator(nullptr, this);
1730#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001731 return iterator(nullptr);
Howard Hinnant39213642013-07-23 22:01:58 +00001732#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001733}
1734
1735template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001736inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001737typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001738__hash_table<_Tp, _Hash, _Equal, _Alloc>::begin() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001739{
Howard Hinnant39213642013-07-23 22:01:58 +00001740#if _LIBCPP_DEBUG_LEVEL >= 2
1741 return const_iterator(__p1_.first().__next_, this);
1742#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001743 return const_iterator(__p1_.first().__next_);
Howard Hinnant39213642013-07-23 22:01:58 +00001744#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001745}
1746
1747template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001748inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001749typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001750__hash_table<_Tp, _Hash, _Equal, _Alloc>::end() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001751{
Howard Hinnant39213642013-07-23 22:01:58 +00001752#if _LIBCPP_DEBUG_LEVEL >= 2
1753 return const_iterator(nullptr, this);
1754#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001755 return const_iterator(nullptr);
Howard Hinnant39213642013-07-23 22:01:58 +00001756#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001757}
1758
1759template <class _Tp, class _Hash, class _Equal, class _Alloc>
1760void
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001761__hash_table<_Tp, _Hash, _Equal, _Alloc>::clear() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001762{
1763 if (size() > 0)
1764 {
1765 __deallocate(__p1_.first().__next_);
1766 __p1_.first().__next_ = nullptr;
1767 size_type __bc = bucket_count();
Howard Hinnant9f66bff2011-07-05 14:14:17 +00001768 for (size_type __i = 0; __i < __bc; ++__i)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001769 __bucket_list_[__i] = nullptr;
1770 size() = 0;
1771 }
1772}
1773
1774template <class _Tp, class _Hash, class _Equal, class _Alloc>
1775pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
1776__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_unique(__node_pointer __nd)
1777{
1778 __nd->__hash_ = hash_function()(__nd->__value_);
1779 size_type __bc = bucket_count();
1780 bool __inserted = false;
Eric Fiselierf6535882016-07-23 20:36:55 +00001781 __next_pointer __ndptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001782 size_t __chash;
1783 if (__bc != 0)
1784 {
Howard Hinnant7a445152012-07-06 17:31:14 +00001785 __chash = __constrain_hash(__nd->__hash_, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001786 __ndptr = __bucket_list_[__chash];
1787 if (__ndptr != nullptr)
1788 {
1789 for (__ndptr = __ndptr->__next_; __ndptr != nullptr &&
Eric Fiselierf6535882016-07-23 20:36:55 +00001790 __constrain_hash(__ndptr->__hash(), __bc) == __chash;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001791 __ndptr = __ndptr->__next_)
1792 {
Eric Fiselierf6535882016-07-23 20:36:55 +00001793 if (key_eq()(__ndptr->__upcast()->__value_, __nd->__value_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001794 goto __done;
1795 }
1796 }
1797 }
1798 {
1799 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1800 {
Eric Fiselier57947ca2015-02-02 21:31:48 +00001801 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001802 size_type(ceil(float(size() + 1) / max_load_factor()))));
1803 __bc = bucket_count();
Howard Hinnant7a445152012-07-06 17:31:14 +00001804 __chash = __constrain_hash(__nd->__hash_, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001805 }
1806 // insert_after __bucket_list_[__chash], or __first_node if bucket is null
Eric Fiselierf6535882016-07-23 20:36:55 +00001807 __next_pointer __pn = __bucket_list_[__chash];
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001808 if (__pn == nullptr)
1809 {
Eric Fiselierf6535882016-07-23 20:36:55 +00001810 __pn =__p1_.first().__ptr();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001811 __nd->__next_ = __pn->__next_;
Eric Fiselierf6535882016-07-23 20:36:55 +00001812 __pn->__next_ = __nd->__ptr();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001813 // fix up __bucket_list_
1814 __bucket_list_[__chash] = __pn;
1815 if (__nd->__next_ != nullptr)
Eric Fiselierf6535882016-07-23 20:36:55 +00001816 __bucket_list_[__constrain_hash(__nd->__next_->__hash(), __bc)] = __nd->__ptr();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001817 }
1818 else
1819 {
1820 __nd->__next_ = __pn->__next_;
Eric Fiselierf6535882016-07-23 20:36:55 +00001821 __pn->__next_ = __nd->__ptr();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001822 }
Eric Fiselierf6535882016-07-23 20:36:55 +00001823 __ndptr = __nd->__ptr();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001824 // increment size
1825 ++size();
1826 __inserted = true;
1827 }
1828__done:
Howard Hinnant39213642013-07-23 22:01:58 +00001829#if _LIBCPP_DEBUG_LEVEL >= 2
1830 return pair<iterator, bool>(iterator(__ndptr, this), __inserted);
1831#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001832 return pair<iterator, bool>(iterator(__ndptr), __inserted);
Howard Hinnant39213642013-07-23 22:01:58 +00001833#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001834}
1835
1836template <class _Tp, class _Hash, class _Equal, class _Alloc>
1837typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1838__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi(__node_pointer __cp)
1839{
1840 __cp->__hash_ = hash_function()(__cp->__value_);
1841 size_type __bc = bucket_count();
1842 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1843 {
Eric Fiselier57947ca2015-02-02 21:31:48 +00001844 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001845 size_type(ceil(float(size() + 1) / max_load_factor()))));
1846 __bc = bucket_count();
1847 }
Howard Hinnant7a445152012-07-06 17:31:14 +00001848 size_t __chash = __constrain_hash(__cp->__hash_, __bc);
Eric Fiselierf6535882016-07-23 20:36:55 +00001849 __next_pointer __pn = __bucket_list_[__chash];
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001850 if (__pn == nullptr)
1851 {
Eric Fiselierf6535882016-07-23 20:36:55 +00001852 __pn =__p1_.first().__ptr();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001853 __cp->__next_ = __pn->__next_;
Eric Fiselierf6535882016-07-23 20:36:55 +00001854 __pn->__next_ = __cp->__ptr();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001855 // fix up __bucket_list_
1856 __bucket_list_[__chash] = __pn;
1857 if (__cp->__next_ != nullptr)
Eric Fiselierf6535882016-07-23 20:36:55 +00001858 __bucket_list_[__constrain_hash(__cp->__next_->__hash(), __bc)]
1859 = __cp->__ptr();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001860 }
1861 else
1862 {
1863 for (bool __found = false; __pn->__next_ != nullptr &&
Eric Fiselierf6535882016-07-23 20:36:55 +00001864 __constrain_hash(__pn->__next_->__hash(), __bc) == __chash;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001865 __pn = __pn->__next_)
Howard Hinnant324bb032010-08-22 00:02:43 +00001866 {
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001867 // __found key_eq() action
1868 // false false loop
1869 // true true loop
1870 // false true set __found to true
1871 // true false break
Eric Fiselierf6535882016-07-23 20:36:55 +00001872 if (__found != (__pn->__next_->__hash() == __cp->__hash_ &&
1873 key_eq()(__pn->__next_->__upcast()->__value_, __cp->__value_)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001874 {
1875 if (!__found)
1876 __found = true;
1877 else
1878 break;
1879 }
1880 }
1881 __cp->__next_ = __pn->__next_;
Eric Fiselierf6535882016-07-23 20:36:55 +00001882 __pn->__next_ = __cp->__ptr();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001883 if (__cp->__next_ != nullptr)
1884 {
Eric Fiselierf6535882016-07-23 20:36:55 +00001885 size_t __nhash = __constrain_hash(__cp->__next_->__hash(), __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001886 if (__nhash != __chash)
Eric Fiselierf6535882016-07-23 20:36:55 +00001887 __bucket_list_[__nhash] = __cp->__ptr();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001888 }
1889 }
1890 ++size();
Howard Hinnant39213642013-07-23 22:01:58 +00001891#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselierf6535882016-07-23 20:36:55 +00001892 return iterator(__cp->__ptr(), this);
Howard Hinnant39213642013-07-23 22:01:58 +00001893#else
Eric Fiselierf6535882016-07-23 20:36:55 +00001894 return iterator(__cp->__ptr());
Howard Hinnant39213642013-07-23 22:01:58 +00001895#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001896}
1897
1898template <class _Tp, class _Hash, class _Equal, class _Alloc>
1899typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1900__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi(
1901 const_iterator __p, __node_pointer __cp)
1902{
Howard Hinnant824c1992013-08-02 17:50:49 +00001903#if _LIBCPP_DEBUG_LEVEL >= 2
1904 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1905 "unordered container::emplace_hint(const_iterator, args...) called with an iterator not"
1906 " referring to this unordered container");
1907#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001908 if (__p != end() && key_eq()(*__p, __cp->__value_))
1909 {
Eric Fiselierf6535882016-07-23 20:36:55 +00001910 __next_pointer __np = __p.__node_;
1911 __cp->__hash_ = __np->__hash();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001912 size_type __bc = bucket_count();
1913 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1914 {
Eric Fiselier57947ca2015-02-02 21:31:48 +00001915 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001916 size_type(ceil(float(size() + 1) / max_load_factor()))));
1917 __bc = bucket_count();
1918 }
Howard Hinnant7a445152012-07-06 17:31:14 +00001919 size_t __chash = __constrain_hash(__cp->__hash_, __bc);
Eric Fiselierf6535882016-07-23 20:36:55 +00001920 __next_pointer __pp = __bucket_list_[__chash];
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001921 while (__pp->__next_ != __np)
1922 __pp = __pp->__next_;
1923 __cp->__next_ = __np;
Eric Fiselierf6535882016-07-23 20:36:55 +00001924 __pp->__next_ = static_cast<__next_pointer>(__cp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001925 ++size();
Howard Hinnant39213642013-07-23 22:01:58 +00001926#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselierf6535882016-07-23 20:36:55 +00001927 return iterator(static_cast<__next_pointer>(__cp), this);
Howard Hinnant39213642013-07-23 22:01:58 +00001928#else
Eric Fiselierf6535882016-07-23 20:36:55 +00001929 return iterator(static_cast<__next_pointer>(__cp));
Howard Hinnant39213642013-07-23 22:01:58 +00001930#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001931 }
1932 return __node_insert_multi(__cp);
1933}
1934
Eric Fiselierfdae69a2015-06-13 07:18:32 +00001935
1936
Eric Fiselier2960ae22016-02-11 11:59:44 +00001937#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierfdae69a2015-06-13 07:18:32 +00001938template <class _Tp, class _Hash, class _Equal, class _Alloc>
Eric Fiselier2960ae22016-02-11 11:59:44 +00001939template <class _Key, class ..._Args>
Eric Fiselierfdae69a2015-06-13 07:18:32 +00001940_LIBCPP_INLINE_VISIBILITY
1941pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
Eric Fiselier2960ae22016-02-11 11:59:44 +00001942__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_key_args(_Key const& __k, _Args&&... __args)
Eric Fiselierfdae69a2015-06-13 07:18:32 +00001943#else
1944template <class _Tp, class _Hash, class _Equal, class _Alloc>
Eric Fiselier2960ae22016-02-11 11:59:44 +00001945template <class _Key, class _Args>
Eric Fiselierfdae69a2015-06-13 07:18:32 +00001946_LIBCPP_INLINE_VISIBILITY
1947pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
Eric Fiselier2960ae22016-02-11 11:59:44 +00001948__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_key_args(_Key const& __k, _Args& __args)
Eric Fiselierfdae69a2015-06-13 07:18:32 +00001949#endif
1950{
Eric Fiselier2960ae22016-02-11 11:59:44 +00001951
1952 size_t __hash = hash_function()(__k);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001953 size_type __bc = bucket_count();
1954 bool __inserted = false;
Eric Fiselierf6535882016-07-23 20:36:55 +00001955 __next_pointer __nd;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001956 size_t __chash;
1957 if (__bc != 0)
1958 {
Howard Hinnant7a445152012-07-06 17:31:14 +00001959 __chash = __constrain_hash(__hash, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001960 __nd = __bucket_list_[__chash];
1961 if (__nd != nullptr)
1962 {
1963 for (__nd = __nd->__next_; __nd != nullptr &&
Eric Fiselierd7570902016-07-24 06:22:25 +00001964 (__nd->__hash() == __hash || __constrain_hash(__nd->__hash(), __bc) == __chash);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001965 __nd = __nd->__next_)
1966 {
Eric Fiselierf6535882016-07-23 20:36:55 +00001967 if (key_eq()(__nd->__upcast()->__value_, __k))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001968 goto __done;
1969 }
1970 }
1971 }
1972 {
Eric Fiselier2960ae22016-02-11 11:59:44 +00001973#ifndef _LIBCPP_CXX03_LANG
1974 __node_holder __h = __construct_node_hash(__hash, _VSTD::forward<_Args>(__args)...);
1975#else
1976 __node_holder __h = __construct_node_hash(__hash, __args);
1977#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001978 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1979 {
Eric Fiselier57947ca2015-02-02 21:31:48 +00001980 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001981 size_type(ceil(float(size() + 1) / max_load_factor()))));
1982 __bc = bucket_count();
Howard Hinnant7a445152012-07-06 17:31:14 +00001983 __chash = __constrain_hash(__hash, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001984 }
1985 // insert_after __bucket_list_[__chash], or __first_node if bucket is null
Eric Fiselierf6535882016-07-23 20:36:55 +00001986 __next_pointer __pn = __bucket_list_[__chash];
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001987 if (__pn == nullptr)
1988 {
Eric Fiselierf6535882016-07-23 20:36:55 +00001989 __pn = __p1_.first().__ptr();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001990 __h->__next_ = __pn->__next_;
Eric Fiselierf6535882016-07-23 20:36:55 +00001991 __pn->__next_ = __h.get()->__ptr();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001992 // fix up __bucket_list_
1993 __bucket_list_[__chash] = __pn;
1994 if (__h->__next_ != nullptr)
Eric Fiselierf6535882016-07-23 20:36:55 +00001995 __bucket_list_[__constrain_hash(__h->__next_->__hash(), __bc)]
1996 = __h.get()->__ptr();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001997 }
1998 else
1999 {
2000 __h->__next_ = __pn->__next_;
Eric Fiselierf6535882016-07-23 20:36:55 +00002001 __pn->__next_ = static_cast<__next_pointer>(__h.get());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002002 }
Eric Fiselierf6535882016-07-23 20:36:55 +00002003 __nd = static_cast<__next_pointer>(__h.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002004 // increment size
2005 ++size();
2006 __inserted = true;
2007 }
2008__done:
Howard Hinnant39213642013-07-23 22:01:58 +00002009#if _LIBCPP_DEBUG_LEVEL >= 2
2010 return pair<iterator, bool>(iterator(__nd, this), __inserted);
2011#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002012 return pair<iterator, bool>(iterator(__nd), __inserted);
Howard Hinnant39213642013-07-23 22:01:58 +00002013#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002014}
2015
Eric Fiselier2960ae22016-02-11 11:59:44 +00002016#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002017
2018template <class _Tp, class _Hash, class _Equal, class _Alloc>
2019template <class... _Args>
2020pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
Duncan P. N. Exon Smith798ec842016-03-17 20:45:20 +00002021__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_impl(_Args&&... __args)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002022{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002023 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002024 pair<iterator, bool> __r = __node_insert_unique(__h.get());
2025 if (__r.second)
2026 __h.release();
2027 return __r;
2028}
2029
2030template <class _Tp, class _Hash, class _Equal, class _Alloc>
2031template <class... _Args>
2032typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2033__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_multi(_Args&&... __args)
2034{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002035 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002036 iterator __r = __node_insert_multi(__h.get());
2037 __h.release();
2038 return __r;
2039}
2040
2041template <class _Tp, class _Hash, class _Equal, class _Alloc>
2042template <class... _Args>
2043typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2044__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_hint_multi(
2045 const_iterator __p, _Args&&... __args)
2046{
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00002047#if _LIBCPP_DEBUG_LEVEL >= 2
2048 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2049 "unordered container::emplace_hint(const_iterator, args...) called with an iterator not"
2050 " referring to this unordered container");
2051#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00002052 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002053 iterator __r = __node_insert_multi(__p, __h.get());
2054 __h.release();
2055 return __r;
2056}
2057
Eric Fiselier2960ae22016-02-11 11:59:44 +00002058#else // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002059
2060template <class _Tp, class _Hash, class _Equal, class _Alloc>
2061typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
Eric Fiselier2960ae22016-02-11 11:59:44 +00002062__hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const __container_value_type& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002063{
2064 __node_holder __h = __construct_node(__x);
2065 iterator __r = __node_insert_multi(__h.get());
2066 __h.release();
2067 return __r;
2068}
2069
2070template <class _Tp, class _Hash, class _Equal, class _Alloc>
2071typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2072__hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const_iterator __p,
Eric Fiselier2960ae22016-02-11 11:59:44 +00002073 const __container_value_type& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002074{
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00002075#if _LIBCPP_DEBUG_LEVEL >= 2
2076 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2077 "unordered container::insert(const_iterator, lvalue) called with an iterator not"
2078 " referring to this unordered container");
2079#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002080 __node_holder __h = __construct_node(__x);
2081 iterator __r = __node_insert_multi(__p, __h.get());
2082 __h.release();
2083 return __r;
2084}
2085
Eric Fiselier2960ae22016-02-11 11:59:44 +00002086#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002087
2088template <class _Tp, class _Hash, class _Equal, class _Alloc>
2089void
2090__hash_table<_Tp, _Hash, _Equal, _Alloc>::rehash(size_type __n)
2091{
Howard Hinnant7a445152012-07-06 17:31:14 +00002092 if (__n == 1)
2093 __n = 2;
2094 else if (__n & (__n - 1))
2095 __n = __next_prime(__n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002096 size_type __bc = bucket_count();
2097 if (__n > __bc)
2098 __rehash(__n);
Howard Hinnant7a445152012-07-06 17:31:14 +00002099 else if (__n < __bc)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002100 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002101 __n = _VSTD::max<size_type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002102 (
2103 __n,
Eric Fiselier57947ca2015-02-02 21:31:48 +00002104 __is_hash_power2(__bc) ? __next_hash_pow2(size_t(ceil(float(size()) / max_load_factor()))) :
2105 __next_prime(size_t(ceil(float(size()) / max_load_factor())))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002106 );
2107 if (__n < __bc)
2108 __rehash(__n);
2109 }
2110}
2111
2112template <class _Tp, class _Hash, class _Equal, class _Alloc>
2113void
2114__hash_table<_Tp, _Hash, _Equal, _Alloc>::__rehash(size_type __nbc)
2115{
Howard Hinnant39213642013-07-23 22:01:58 +00002116#if _LIBCPP_DEBUG_LEVEL >= 2
2117 __get_db()->__invalidate_all(this);
2118#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002119 __pointer_allocator& __npa = __bucket_list_.get_deleter().__alloc();
2120 __bucket_list_.reset(__nbc > 0 ?
2121 __pointer_alloc_traits::allocate(__npa, __nbc) : nullptr);
2122 __bucket_list_.get_deleter().size() = __nbc;
2123 if (__nbc > 0)
2124 {
2125 for (size_type __i = 0; __i < __nbc; ++__i)
2126 __bucket_list_[__i] = nullptr;
Eric Fiselierf6535882016-07-23 20:36:55 +00002127 __next_pointer __pp = __p1_.first().__ptr();
2128 __next_pointer __cp = __pp->__next_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002129 if (__cp != nullptr)
2130 {
Eric Fiselierf6535882016-07-23 20:36:55 +00002131 size_type __chash = __constrain_hash(__cp->__hash(), __nbc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002132 __bucket_list_[__chash] = __pp;
2133 size_type __phash = __chash;
2134 for (__pp = __cp, __cp = __cp->__next_; __cp != nullptr;
2135 __cp = __pp->__next_)
2136 {
Eric Fiselierf6535882016-07-23 20:36:55 +00002137 __chash = __constrain_hash(__cp->__hash(), __nbc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002138 if (__chash == __phash)
2139 __pp = __cp;
2140 else
2141 {
2142 if (__bucket_list_[__chash] == nullptr)
2143 {
2144 __bucket_list_[__chash] = __pp;
2145 __pp = __cp;
2146 __phash = __chash;
2147 }
2148 else
2149 {
Eric Fiselierf6535882016-07-23 20:36:55 +00002150 __next_pointer __np = __cp;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002151 for (; __np->__next_ != nullptr &&
Eric Fiselierf6535882016-07-23 20:36:55 +00002152 key_eq()(__cp->__upcast()->__value_,
2153 __np->__next_->__upcast()->__value_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002154 __np = __np->__next_)
2155 ;
2156 __pp->__next_ = __np->__next_;
2157 __np->__next_ = __bucket_list_[__chash]->__next_;
2158 __bucket_list_[__chash]->__next_ = __cp;
Howard Hinnant324bb032010-08-22 00:02:43 +00002159
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002160 }
2161 }
2162 }
2163 }
2164 }
2165}
2166
2167template <class _Tp, class _Hash, class _Equal, class _Alloc>
2168template <class _Key>
2169typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2170__hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k)
2171{
2172 size_t __hash = hash_function()(__k);
2173 size_type __bc = bucket_count();
2174 if (__bc != 0)
2175 {
Howard Hinnant7a445152012-07-06 17:31:14 +00002176 size_t __chash = __constrain_hash(__hash, __bc);
Eric Fiselierf6535882016-07-23 20:36:55 +00002177 __next_pointer __nd = __bucket_list_[__chash];
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002178 if (__nd != nullptr)
2179 {
2180 for (__nd = __nd->__next_; __nd != nullptr &&
Eric Fiselierf6535882016-07-23 20:36:55 +00002181 (__nd->__hash() == __hash
2182 || __constrain_hash(__nd->__hash(), __bc) == __chash);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002183 __nd = __nd->__next_)
2184 {
Eric Fiselierf6535882016-07-23 20:36:55 +00002185 if ((__nd->__hash() == __hash)
2186 && key_eq()(__nd->__upcast()->__value_, __k))
Howard Hinnant39213642013-07-23 22:01:58 +00002187#if _LIBCPP_DEBUG_LEVEL >= 2
2188 return iterator(__nd, this);
2189#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002190 return iterator(__nd);
Howard Hinnant39213642013-07-23 22:01:58 +00002191#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002192 }
2193 }
2194 }
2195 return end();
2196}
2197
2198template <class _Tp, class _Hash, class _Equal, class _Alloc>
2199template <class _Key>
2200typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
2201__hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k) const
2202{
2203 size_t __hash = hash_function()(__k);
2204 size_type __bc = bucket_count();
2205 if (__bc != 0)
2206 {
Howard Hinnant7a445152012-07-06 17:31:14 +00002207 size_t __chash = __constrain_hash(__hash, __bc);
Eric Fiselierf6535882016-07-23 20:36:55 +00002208 __next_pointer __nd = __bucket_list_[__chash];
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002209 if (__nd != nullptr)
2210 {
2211 for (__nd = __nd->__next_; __nd != nullptr &&
Eric Fiselierf6535882016-07-23 20:36:55 +00002212 (__hash == __nd->__hash()
2213 || __constrain_hash(__nd->__hash(), __bc) == __chash);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002214 __nd = __nd->__next_)
2215 {
Eric Fiselierf6535882016-07-23 20:36:55 +00002216 if ((__nd->__hash() == __hash)
2217 && key_eq()(__nd->__upcast()->__value_, __k))
Howard Hinnant39213642013-07-23 22:01:58 +00002218#if _LIBCPP_DEBUG_LEVEL >= 2
2219 return const_iterator(__nd, this);
2220#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002221 return const_iterator(__nd);
Howard Hinnant39213642013-07-23 22:01:58 +00002222#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002223 }
2224 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002225
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002226 }
2227 return end();
2228}
2229
Eric Fiselier2960ae22016-02-11 11:59:44 +00002230#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002231
2232template <class _Tp, class _Hash, class _Equal, class _Alloc>
2233template <class ..._Args>
2234typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
2235__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(_Args&& ...__args)
2236{
Eric Fiselier2960ae22016-02-11 11:59:44 +00002237 static_assert(!__is_hash_value_type<_Args...>::value,
2238 "Construct cannot be called with a hash value type");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002239 __node_allocator& __na = __node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00002240 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselier2960ae22016-02-11 11:59:44 +00002241 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002242 __h.get_deleter().__value_constructed = true;
2243 __h->__hash_ = hash_function()(__h->__value_);
2244 __h->__next_ = nullptr;
2245 return __h;
2246}
2247
2248template <class _Tp, class _Hash, class _Equal, class _Alloc>
Eric Fiselier2960ae22016-02-11 11:59:44 +00002249template <class _First, class ..._Rest>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002250typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Eric Fiselier2960ae22016-02-11 11:59:44 +00002251__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node_hash(
2252 size_t __hash, _First&& __f, _Rest&& ...__rest)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002253{
Eric Fiselier2960ae22016-02-11 11:59:44 +00002254 static_assert(!__is_hash_value_type<_First, _Rest...>::value,
2255 "Construct cannot be called with a hash value type");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002256 __node_allocator& __na = __node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00002257 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselier2960ae22016-02-11 11:59:44 +00002258 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_),
2259 _VSTD::forward<_First>(__f),
2260 _VSTD::forward<_Rest>(__rest)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002261 __h.get_deleter().__value_constructed = true;
2262 __h->__hash_ = __hash;
2263 __h->__next_ = nullptr;
Howard Hinnant9a894d92013-08-22 18:29:50 +00002264 return __h;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002265}
2266
Eric Fiselier2960ae22016-02-11 11:59:44 +00002267#else // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002268
2269template <class _Tp, class _Hash, class _Equal, class _Alloc>
2270typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Eric Fiselier2960ae22016-02-11 11:59:44 +00002271__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(const __container_value_type& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002272{
2273 __node_allocator& __na = __node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00002274 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselier2960ae22016-02-11 11:59:44 +00002275 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002276 __h.get_deleter().__value_constructed = true;
2277 __h->__hash_ = hash_function()(__h->__value_);
2278 __h->__next_ = nullptr;
Dimitry Andric89663502015-08-19 06:43:33 +00002279 return _LIBCPP_EXPLICIT_MOVE(__h); // explicitly moved for C++03
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002280}
2281
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002282template <class _Tp, class _Hash, class _Equal, class _Alloc>
2283typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Eric Fiselier2960ae22016-02-11 11:59:44 +00002284__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node_hash(size_t __hash,
2285 const __container_value_type& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002286{
2287 __node_allocator& __na = __node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00002288 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselier2960ae22016-02-11 11:59:44 +00002289 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002290 __h.get_deleter().__value_constructed = true;
2291 __h->__hash_ = __hash;
2292 __h->__next_ = nullptr;
Dimitry Andric89663502015-08-19 06:43:33 +00002293 return _LIBCPP_EXPLICIT_MOVE(__h); // explicitly moved for C++03
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002294}
2295
Eric Fiselier2960ae22016-02-11 11:59:44 +00002296#endif // _LIBCPP_CXX03_LANG
2297
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002298template <class _Tp, class _Hash, class _Equal, class _Alloc>
2299typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2300__hash_table<_Tp, _Hash, _Equal, _Alloc>::erase(const_iterator __p)
2301{
Eric Fiselierf6535882016-07-23 20:36:55 +00002302 __next_pointer __np = __p.__node_;
Howard Hinnant39213642013-07-23 22:01:58 +00002303#if _LIBCPP_DEBUG_LEVEL >= 2
2304 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2305 "unordered container erase(iterator) called with an iterator not"
2306 " referring to this container");
2307 _LIBCPP_ASSERT(__p != end(),
2308 "unordered container erase(iterator) called with a non-dereferenceable iterator");
2309 iterator __r(__np, this);
2310#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002311 iterator __r(__np);
Howard Hinnant39213642013-07-23 22:01:58 +00002312#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002313 ++__r;
2314 remove(__p);
2315 return __r;
2316}
2317
2318template <class _Tp, class _Hash, class _Equal, class _Alloc>
2319typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2320__hash_table<_Tp, _Hash, _Equal, _Alloc>::erase(const_iterator __first,
2321 const_iterator __last)
2322{
Howard Hinnant39213642013-07-23 22:01:58 +00002323#if _LIBCPP_DEBUG_LEVEL >= 2
2324 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
2325 "unodered container::erase(iterator, iterator) called with an iterator not"
2326 " referring to this unodered container");
2327 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__last) == this,
2328 "unodered container::erase(iterator, iterator) called with an iterator not"
2329 " referring to this unodered container");
2330#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002331 for (const_iterator __p = __first; __first != __last; __p = __first)
2332 {
2333 ++__first;
2334 erase(__p);
2335 }
Eric Fiselierf6535882016-07-23 20:36:55 +00002336 __next_pointer __np = __last.__node_;
Howard Hinnant39213642013-07-23 22:01:58 +00002337#if _LIBCPP_DEBUG_LEVEL >= 2
2338 return iterator (__np, this);
2339#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002340 return iterator (__np);
Howard Hinnant39213642013-07-23 22:01:58 +00002341#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002342}
2343
2344template <class _Tp, class _Hash, class _Equal, class _Alloc>
2345template <class _Key>
2346typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2347__hash_table<_Tp, _Hash, _Equal, _Alloc>::__erase_unique(const _Key& __k)
2348{
2349 iterator __i = find(__k);
2350 if (__i == end())
2351 return 0;
2352 erase(__i);
2353 return 1;
2354}
2355
2356template <class _Tp, class _Hash, class _Equal, class _Alloc>
2357template <class _Key>
2358typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2359__hash_table<_Tp, _Hash, _Equal, _Alloc>::__erase_multi(const _Key& __k)
2360{
2361 size_type __r = 0;
2362 iterator __i = find(__k);
2363 if (__i != end())
2364 {
2365 iterator __e = end();
2366 do
2367 {
2368 erase(__i++);
2369 ++__r;
2370 } while (__i != __e && key_eq()(*__i, __k));
2371 }
2372 return __r;
2373}
2374
2375template <class _Tp, class _Hash, class _Equal, class _Alloc>
2376typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00002377__hash_table<_Tp, _Hash, _Equal, _Alloc>::remove(const_iterator __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002378{
2379 // current node
Eric Fiselierf6535882016-07-23 20:36:55 +00002380 __next_pointer __cn = __p.__node_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002381 size_type __bc = bucket_count();
Eric Fiselierf6535882016-07-23 20:36:55 +00002382 size_t __chash = __constrain_hash(__cn->__hash(), __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002383 // find previous node
Eric Fiselierf6535882016-07-23 20:36:55 +00002384 __next_pointer __pn = __bucket_list_[__chash];
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002385 for (; __pn->__next_ != __cn; __pn = __pn->__next_)
2386 ;
2387 // Fix up __bucket_list_
2388 // if __pn is not in same bucket (before begin is not in same bucket) &&
2389 // if __cn->__next_ is not in same bucket (nullptr is not in same bucket)
Eric Fiselierf6535882016-07-23 20:36:55 +00002390 if (__pn == __p1_.first().__ptr()
2391 || __constrain_hash(__pn->__hash(), __bc) != __chash)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002392 {
Eric Fiselierf6535882016-07-23 20:36:55 +00002393 if (__cn->__next_ == nullptr
2394 || __constrain_hash(__cn->__next_->__hash(), __bc) != __chash)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002395 __bucket_list_[__chash] = nullptr;
2396 }
2397 // if __cn->__next_ is not in same bucket (nullptr is in same bucket)
2398 if (__cn->__next_ != nullptr)
2399 {
Eric Fiselierf6535882016-07-23 20:36:55 +00002400 size_t __nhash = __constrain_hash(__cn->__next_->__hash(), __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002401 if (__nhash != __chash)
2402 __bucket_list_[__nhash] = __pn;
2403 }
2404 // remove __cn
2405 __pn->__next_ = __cn->__next_;
2406 __cn->__next_ = nullptr;
2407 --size();
Howard Hinnant39213642013-07-23 22:01:58 +00002408#if _LIBCPP_DEBUG_LEVEL >= 2
2409 __c_node* __c = __get_db()->__find_c_and_lock(this);
2410 for (__i_node** __p = __c->end_; __p != __c->beg_; )
2411 {
2412 --__p;
2413 iterator* __i = static_cast<iterator*>((*__p)->__i_);
2414 if (__i->__node_ == __cn)
2415 {
2416 (*__p)->__c_ = nullptr;
2417 if (--__c->end_ != __p)
2418 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
2419 }
2420 }
2421 __get_db()->unlock();
2422#endif
Eric Fiselierf6535882016-07-23 20:36:55 +00002423 return __node_holder(__cn->__upcast(), _Dp(__node_alloc(), true));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002424}
2425
2426template <class _Tp, class _Hash, class _Equal, class _Alloc>
2427template <class _Key>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00002428inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002429typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2430__hash_table<_Tp, _Hash, _Equal, _Alloc>::__count_unique(const _Key& __k) const
2431{
2432 return static_cast<size_type>(find(__k) != end());
2433}
2434
2435template <class _Tp, class _Hash, class _Equal, class _Alloc>
2436template <class _Key>
2437typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2438__hash_table<_Tp, _Hash, _Equal, _Alloc>::__count_multi(const _Key& __k) const
2439{
2440 size_type __r = 0;
2441 const_iterator __i = find(__k);
2442 if (__i != end())
2443 {
2444 const_iterator __e = end();
2445 do
2446 {
2447 ++__i;
2448 ++__r;
2449 } while (__i != __e && key_eq()(*__i, __k));
2450 }
2451 return __r;
2452}
2453
2454template <class _Tp, class _Hash, class _Equal, class _Alloc>
2455template <class _Key>
2456pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator,
2457 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator>
2458__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_unique(
2459 const _Key& __k)
2460{
2461 iterator __i = find(__k);
2462 iterator __j = __i;
2463 if (__i != end())
2464 ++__j;
2465 return pair<iterator, iterator>(__i, __j);
2466}
2467
2468template <class _Tp, class _Hash, class _Equal, class _Alloc>
2469template <class _Key>
2470pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator,
2471 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator>
2472__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_unique(
2473 const _Key& __k) const
2474{
2475 const_iterator __i = find(__k);
2476 const_iterator __j = __i;
2477 if (__i != end())
2478 ++__j;
2479 return pair<const_iterator, const_iterator>(__i, __j);
2480}
2481
2482template <class _Tp, class _Hash, class _Equal, class _Alloc>
2483template <class _Key>
2484pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator,
2485 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator>
2486__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_multi(
2487 const _Key& __k)
2488{
2489 iterator __i = find(__k);
2490 iterator __j = __i;
2491 if (__i != end())
2492 {
2493 iterator __e = end();
2494 do
2495 {
2496 ++__j;
2497 } while (__j != __e && key_eq()(*__j, __k));
2498 }
2499 return pair<iterator, iterator>(__i, __j);
2500}
2501
2502template <class _Tp, class _Hash, class _Equal, class _Alloc>
2503template <class _Key>
2504pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator,
2505 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator>
2506__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_multi(
2507 const _Key& __k) const
2508{
2509 const_iterator __i = find(__k);
2510 const_iterator __j = __i;
2511 if (__i != end())
2512 {
2513 const_iterator __e = end();
2514 do
2515 {
2516 ++__j;
2517 } while (__j != __e && key_eq()(*__j, __k));
2518 }
2519 return pair<const_iterator, const_iterator>(__i, __j);
2520}
2521
2522template <class _Tp, class _Hash, class _Equal, class _Alloc>
2523void
2524__hash_table<_Tp, _Hash, _Equal, _Alloc>::swap(__hash_table& __u)
Eric Fiselier692177d2015-07-18 20:40:46 +00002525#if _LIBCPP_STD_VER <= 11
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00002526 _NOEXCEPT_(
Marshall Clow7d914d12015-07-13 20:04:56 +00002527 __is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value
Marshall Clow7d914d12015-07-13 20:04:56 +00002528 && (!allocator_traits<__pointer_allocator>::propagate_on_container_swap::value
2529 || __is_nothrow_swappable<__pointer_allocator>::value)
2530 && (!__node_traits::propagate_on_container_swap::value
2531 || __is_nothrow_swappable<__node_allocator>::value)
Marshall Clow7d914d12015-07-13 20:04:56 +00002532 )
Eric Fiselier692177d2015-07-18 20:40:46 +00002533#else
2534 _NOEXCEPT_(__is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value)
2535#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002536{
2537 {
2538 __node_pointer_pointer __npp = __bucket_list_.release();
2539 __bucket_list_.reset(__u.__bucket_list_.release());
2540 __u.__bucket_list_.reset(__npp);
2541 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002542 _VSTD::swap(__bucket_list_.get_deleter().size(), __u.__bucket_list_.get_deleter().size());
Marshall Clow7d914d12015-07-13 20:04:56 +00002543 __swap_allocator(__bucket_list_.get_deleter().__alloc(),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002544 __u.__bucket_list_.get_deleter().__alloc());
Marshall Clow7d914d12015-07-13 20:04:56 +00002545 __swap_allocator(__node_alloc(), __u.__node_alloc());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002546 _VSTD::swap(__p1_.first().__next_, __u.__p1_.first().__next_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002547 __p2_.swap(__u.__p2_);
2548 __p3_.swap(__u.__p3_);
2549 if (size() > 0)
Eric Fiselierf6535882016-07-23 20:36:55 +00002550 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash(), bucket_count())] =
2551 __p1_.first().__ptr();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002552 if (__u.size() > 0)
Eric Fiselierf6535882016-07-23 20:36:55 +00002553 __u.__bucket_list_[__constrain_hash(__u.__p1_.first().__next_->__hash(), __u.bucket_count())] =
2554 __u.__p1_.first().__ptr();
Howard Hinnant39213642013-07-23 22:01:58 +00002555#if _LIBCPP_DEBUG_LEVEL >= 2
2556 __get_db()->swap(this, &__u);
2557#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002558}
2559
2560template <class _Tp, class _Hash, class _Equal, class _Alloc>
2561typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2562__hash_table<_Tp, _Hash, _Equal, _Alloc>::bucket_size(size_type __n) const
2563{
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00002564 _LIBCPP_ASSERT(__n < bucket_count(),
2565 "unordered container::bucket_size(n) called with n >= bucket_count()");
Eric Fiselierf6535882016-07-23 20:36:55 +00002566 __next_pointer __np = __bucket_list_[__n];
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002567 size_type __bc = bucket_count();
2568 size_type __r = 0;
2569 if (__np != nullptr)
2570 {
2571 for (__np = __np->__next_; __np != nullptr &&
Eric Fiselierf6535882016-07-23 20:36:55 +00002572 __constrain_hash(__np->__hash(), __bc) == __n;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002573 __np = __np->__next_, ++__r)
2574 ;
2575 }
2576 return __r;
2577}
2578
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00002579template <class _Tp, class _Hash, class _Equal, class _Alloc>
2580inline _LIBCPP_INLINE_VISIBILITY
2581void
2582swap(__hash_table<_Tp, _Hash, _Equal, _Alloc>& __x,
2583 __hash_table<_Tp, _Hash, _Equal, _Alloc>& __y)
2584 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
2585{
2586 __x.swap(__y);
2587}
2588
Howard Hinnant39213642013-07-23 22:01:58 +00002589#if _LIBCPP_DEBUG_LEVEL >= 2
2590
2591template <class _Tp, class _Hash, class _Equal, class _Alloc>
2592bool
2593__hash_table<_Tp, _Hash, _Equal, _Alloc>::__dereferenceable(const const_iterator* __i) const
2594{
2595 return __i->__node_ != nullptr;
2596}
2597
2598template <class _Tp, class _Hash, class _Equal, class _Alloc>
2599bool
2600__hash_table<_Tp, _Hash, _Equal, _Alloc>::__decrementable(const const_iterator*) const
2601{
2602 return false;
2603}
2604
2605template <class _Tp, class _Hash, class _Equal, class _Alloc>
2606bool
2607__hash_table<_Tp, _Hash, _Equal, _Alloc>::__addable(const const_iterator*, ptrdiff_t) const
2608{
2609 return false;
2610}
2611
2612template <class _Tp, class _Hash, class _Equal, class _Alloc>
2613bool
2614__hash_table<_Tp, _Hash, _Equal, _Alloc>::__subscriptable(const const_iterator*, ptrdiff_t) const
2615{
2616 return false;
2617}
2618
2619#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002620_LIBCPP_END_NAMESPACE_STD
2621
2622#endif // _LIBCPP__HASH_TABLE