blob: 0dc188b7a1f41359904ceb1e0bb6b8c585b673a5 [file] [log] [blame]
Howard Hinnant3e519522010-05-11 19:42:16 +00001// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
Howard Hinnant5b08a8a2010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnant3e519522010-05-11 19:42:16 +00005//
Howard Hinnant412dbeb2010-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 Hinnant3e519522010-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 Fiselier75d0dcf2016-02-10 20:46:23 +000020#include <utility>
Howard Hinnant3e519522010-05-11 19:42:16 +000021
Howard Hinnantab4f4382011-11-29 16:45:27 +000022#include <__undef_min_max>
Saleem Abdulrasool8e5ce332015-02-13 22:15:32 +000023#include <__undef___deallocate>
Howard Hinnantab4f4382011-11-29 16:45:27 +000024
Eric Fiselierc1bd9192014-08-10 23:53:08 +000025#include <__debug>
Howard Hinnant42a30462013-08-02 00:26:35 +000026
Howard Hinnant073458b2011-10-17 20:05:10 +000027#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnant3e519522010-05-11 19:42:16 +000028#pragma GCC system_header
Howard Hinnant073458b2011-10-17 20:05:10 +000029#endif
Howard Hinnant3e519522010-05-11 19:42:16 +000030
31_LIBCPP_BEGIN_NAMESPACE_STD
32
Eric Fiselierfcd02212016-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 Hinnant6e412562013-03-06 23:30:19 +000056_LIBCPP_FUNC_VIS
Howard Hinnantce534202011-06-14 19:58:17 +000057size_t __next_prime(size_t __n);
Howard Hinnant3e519522010-05-11 19:42:16 +000058
59template <class _NodePtr>
60struct __hash_node_base
61{
Eric Fiselier40492ba2016-07-23 20:36:55 +000062 typedef typename pointer_traits<_NodePtr>::element_type __node_type;
Howard Hinnant3e519522010-05-11 19:42:16 +000063 typedef __hash_node_base __first_node;
Eric Fiselier40492ba2016-07-23 20:36:55 +000064 typedef typename __rebind_pointer<_NodePtr, __first_node>::type __node_base_pointer;
65 typedef _NodePtr __node_pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +000066
Eric Fiselier40492ba2016-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 Hinnant3e519522010-05-11 19:42:16 +000094
Howard Hinnant37141072011-06-04 18:54:24 +000095 _LIBCPP_INLINE_VISIBILITY __hash_node_base() _NOEXCEPT : __next_(nullptr) {}
Howard Hinnant3e519522010-05-11 19:42:16 +000096};
97
98template <class _Tp, class _VoidPtr>
99struct __hash_node
100 : public __hash_node_base
101 <
Eric Fiselier934b0922015-12-30 21:52:00 +0000102 typename __rebind_pointer<_VoidPtr, __hash_node<_Tp, _VoidPtr> >::type
Howard Hinnant3e519522010-05-11 19:42:16 +0000103 >
104{
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000105 typedef _Tp __node_value_type;
Howard Hinnant3e519522010-05-11 19:42:16 +0000106
Eric Fiselier40492ba2016-07-23 20:36:55 +0000107 size_t __hash_;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000108 __node_value_type __value_;
Howard Hinnant3e519522010-05-11 19:42:16 +0000109};
110
Howard Hinnant4cb38a82012-07-06 17:31:14 +0000111inline _LIBCPP_INLINE_VISIBILITY
112bool
Eric Fiselier9ba5c112015-02-02 21:31:48 +0000113__is_hash_power2(size_t __bc)
Howard Hinnant4cb38a82012-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 Fiselier118cb412016-07-11 22:02:02 +0000122 return !(__bc & (__bc - 1)) ? __h & (__bc - 1) :
123 (__h < __bc ? __h : __h % __bc);
Howard Hinnant4cb38a82012-07-06 17:31:14 +0000124}
125
126inline _LIBCPP_INLINE_VISIBILITY
127size_t
Eric Fiselier9ba5c112015-02-02 21:31:48 +0000128__next_hash_pow2(size_t __n)
Howard Hinnant4cb38a82012-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 Smithfde79b42016-03-17 20:45:20 +0000133
Howard Hinnantce534202011-06-14 19:58:17 +0000134template <class _Tp, class _Hash, class _Equal, class _Alloc> class __hash_table;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000135
136template <class _NodePtr> class _LIBCPP_TYPE_VIS_ONLY __hash_iterator;
Howard Hinnantf0544c22013-08-12 18:38:34 +0000137template <class _ConstNodePtr> class _LIBCPP_TYPE_VIS_ONLY __hash_const_iterator;
Eric Fiselier75d0dcf2016-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 Hinnantf0544c22013-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 Hinnant3e519522010-05-11 19:42:16 +0000142
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000143template <class _Tp>
Eric Fiselier43b121d2016-02-20 07:59:16 +0000144struct __hash_key_value_types {
Eric Fiselier75d0dcf2016-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 Fiselierfcd02212016-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 Fiselier75d0dcf2016-02-10 20:46:23 +0000169};
170
171template <class _Key, class _Tp>
Eric Fiselier43b121d2016-02-20 07:59:16 +0000172struct __hash_key_value_types<__hash_value_type<_Key, _Tp> > {
Eric Fiselier75d0dcf2016-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 Fiselierfcd02212016-02-11 11:59:44 +0000177 typedef pair<_Key, _Tp> __nc_value_type;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000178 typedef __container_value_type __map_value_type;
179 static const bool __is_map = true;
Eric Fiselierfcd02212016-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 Fiselier75d0dcf2016-02-10 20:46:23 +0000213};
214
Eric Fiselier43b121d2016-02-20 07:59:16 +0000215template <class _Tp, class _AllocPtr, class _KVTypes = __hash_key_value_types<_Tp>,
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000216 bool = _KVTypes::__is_map>
Eric Fiselier43b121d2016-02-20 07:59:16 +0000217struct __hash_map_pointer_types {};
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000218
219template <class _Tp, class _AllocPtr, class _KVTypes>
Eric Fiselier43b121d2016-02-20 07:59:16 +0000220struct __hash_map_pointer_types<_Tp, _AllocPtr, _KVTypes, true> {
Eric Fiselier75d0dcf2016-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 Fiselier43b121d2016-02-20 07:59:16 +0000233 : public __hash_key_value_types<_Tp>, __hash_map_pointer_types<_Tp, _VoidPtr>
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000234
235{
Eric Fiselier43b121d2016-02-20 07:59:16 +0000236 typedef __hash_key_value_types<_Tp> __base;
Eric Fiselier75d0dcf2016-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 Fiselier40492ba2016-07-23 20:36:55 +0000251 typedef typename __node_base_type::__next_pointer __next_pointer;
252
Eric Fiselier75d0dcf2016-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 Fiselier40492ba2016-07-23 20:36:55 +0000258
Eric Fiselier75d0dcf2016-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 Fiselier75d0dcf2016-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 Hinnant3e519522010-05-11 19:42:16 +0000287template <class _NodePtr>
Howard Hinnantf0544c22013-08-12 18:38:34 +0000288class _LIBCPP_TYPE_VIS_ONLY __hash_iterator
Howard Hinnant3e519522010-05-11 19:42:16 +0000289{
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000290 typedef __hash_node_types<_NodePtr> _NodeTypes;
Eric Fiselier40492ba2016-07-23 20:36:55 +0000291 typedef _NodePtr __node_pointer;
292 typedef typename _NodeTypes::__next_pointer __next_pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000293
Eric Fiselier40492ba2016-07-23 20:36:55 +0000294 __next_pointer __node_;
Howard Hinnant3e519522010-05-11 19:42:16 +0000295
296public:
Eric Fiselier75d0dcf2016-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 Hinnant3e519522010-05-11 19:42:16 +0000302
Eric Fiselier40492ba2016-07-23 20:36:55 +0000303 _LIBCPP_INLINE_VISIBILITY __hash_iterator() _NOEXCEPT : __node_(nullptr) {
304 _LIBCPP_DEBUG_MODE(__get_db()->__insert_i(this));
Howard Hinnantb24c8022013-07-23 22:01:58 +0000305 }
306
307#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant43d99232010-09-21 17:32:39 +0000308 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb24c8022013-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 Hinnant43d99232010-09-21 17:32:39 +0000315 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb24c8022013-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 Hinnantb24c8022013-07-23 22:01:58 +0000331#endif // _LIBCPP_DEBUG_LEVEL >= 2
332
333 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-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 Hinnant3e519522010-05-11 19:42:16 +0000339
Howard Hinnant43d99232010-09-21 17:32:39 +0000340 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-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 Hinnantb24c8022013-07-23 22:01:58 +0000350 "Attempted to increment non-incrementable unordered container iterator");
Howard Hinnant3e519522010-05-11 19:42:16 +0000351 __node_ = __node_->__next_;
352 return *this;
353 }
354
Howard Hinnant43d99232010-09-21 17:32:39 +0000355 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000356 __hash_iterator operator++(int)
357 {
358 __hash_iterator __t(*this);
359 ++(*this);
360 return __t;
361 }
362
Howard Hinnant43d99232010-09-21 17:32:39 +0000363 friend _LIBCPP_INLINE_VISIBILITY
364 bool operator==(const __hash_iterator& __x, const __hash_iterator& __y)
Howard Hinnantb24c8022013-07-23 22:01:58 +0000365 {
Howard Hinnantb24c8022013-07-23 22:01:58 +0000366 return __x.__node_ == __y.__node_;
367 }
Howard Hinnant43d99232010-09-21 17:32:39 +0000368 friend _LIBCPP_INLINE_VISIBILITY
369 bool operator!=(const __hash_iterator& __x, const __hash_iterator& __y)
Howard Hinnantb24c8022013-07-23 22:01:58 +0000370 {return !(__x == __y);}
Howard Hinnant3e519522010-05-11 19:42:16 +0000371
372private:
Howard Hinnantb24c8022013-07-23 22:01:58 +0000373#if _LIBCPP_DEBUG_LEVEL >= 2
374 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000375 __hash_iterator(__next_pointer __node, const void* __c) _NOEXCEPT
Howard Hinnantb24c8022013-07-23 22:01:58 +0000376 : __node_(__node)
377 {
378 __get_db()->__insert_ic(this, __c);
379 }
380#else
Howard Hinnant43d99232010-09-21 17:32:39 +0000381 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000382 __hash_iterator(__next_pointer __node) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000383 : __node_(__node)
384 {}
Howard Hinnantb24c8022013-07-23 22:01:58 +0000385#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000386 template <class, class, class, class> friend class __hash_table;
Howard Hinnantf0544c22013-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 Hinnant3e519522010-05-11 19:42:16 +0000391};
392
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000393template <class _NodePtr>
Howard Hinnantf0544c22013-08-12 18:38:34 +0000394class _LIBCPP_TYPE_VIS_ONLY __hash_const_iterator
Howard Hinnant3e519522010-05-11 19:42:16 +0000395{
Eric Fiselier75d0dcf2016-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 Fiselier40492ba2016-07-23 20:36:55 +0000398 typedef _NodePtr __node_pointer;
399 typedef typename _NodeTypes::__next_pointer __next_pointer;
Eric Fiselier757373e2016-02-18 00:20:34 +0000400
Eric Fiselier40492ba2016-07-23 20:36:55 +0000401 __next_pointer __node_;
Howard Hinnant3e519522010-05-11 19:42:16 +0000402
403public:
Eric Fiselier757373e2016-02-18 00:20:34 +0000404 typedef __hash_iterator<_NodePtr> __non_const_iterator;
405
Eric Fiselier75d0dcf2016-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 Hinnant3e519522010-05-11 19:42:16 +0000412
Eric Fiselier40492ba2016-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 Hinnantb24c8022013-07-23 22:01:58 +0000415 }
Eric Fiselier40492ba2016-07-23 20:36:55 +0000416
Howard Hinnant43d99232010-09-21 17:32:39 +0000417 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +0000418 __hash_const_iterator(const __non_const_iterator& __x) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000419 : __node_(__x.__node_)
Howard Hinnantb24c8022013-07-23 22:01:58 +0000420 {
Eric Fiselier40492ba2016-07-23 20:36:55 +0000421 _LIBCPP_DEBUG_MODE(__get_db()->__iterator_copy(this, &__x));
Howard Hinnantb24c8022013-07-23 22:01:58 +0000422 }
423
424#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant43d99232010-09-21 17:32:39 +0000425 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb24c8022013-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 Hinnant43d99232010-09-21 17:32:39 +0000432 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb24c8022013-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 Hinnantb24c8022013-07-23 22:01:58 +0000448#endif // _LIBCPP_DEBUG_LEVEL >= 2
449
450 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000451 reference operator*() const {
452 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
Howard Hinnantb24c8022013-07-23 22:01:58 +0000453 "Attempted to dereference a non-dereferenceable unordered container const_iterator");
Eric Fiselier40492ba2016-07-23 20:36:55 +0000454 return __node_->__upcast()->__value_;
455 }
Howard Hinnantb24c8022013-07-23 22:01:58 +0000456 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000457 pointer operator->() const {
458 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
Howard Hinnantb24c8022013-07-23 22:01:58 +0000459 "Attempted to dereference a non-dereferenceable unordered container const_iterator");
Eric Fiselier40492ba2016-07-23 20:36:55 +0000460 return pointer_traits<pointer>::pointer_to(__node_->__upcast()->__value_);
461 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000462
Howard Hinnant43d99232010-09-21 17:32:39 +0000463 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-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 Hinnant3e519522010-05-11 19:42:16 +0000467 __node_ = __node_->__next_;
468 return *this;
469 }
470
Howard Hinnant43d99232010-09-21 17:32:39 +0000471 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-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 Hinnant43d99232010-09-21 17:32:39 +0000479 friend _LIBCPP_INLINE_VISIBILITY
480 bool operator==(const __hash_const_iterator& __x, const __hash_const_iterator& __y)
Howard Hinnantb24c8022013-07-23 22:01:58 +0000481 {
Howard Hinnantb24c8022013-07-23 22:01:58 +0000482 return __x.__node_ == __y.__node_;
483 }
Howard Hinnant43d99232010-09-21 17:32:39 +0000484 friend _LIBCPP_INLINE_VISIBILITY
485 bool operator!=(const __hash_const_iterator& __x, const __hash_const_iterator& __y)
Howard Hinnantb24c8022013-07-23 22:01:58 +0000486 {return !(__x == __y);}
Howard Hinnant3e519522010-05-11 19:42:16 +0000487
488private:
Howard Hinnantb24c8022013-07-23 22:01:58 +0000489#if _LIBCPP_DEBUG_LEVEL >= 2
490 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000491 __hash_const_iterator(__next_pointer __node, const void* __c) _NOEXCEPT
Howard Hinnantb24c8022013-07-23 22:01:58 +0000492 : __node_(__node)
493 {
494 __get_db()->__insert_ic(this, __c);
495 }
496#else
Howard Hinnant43d99232010-09-21 17:32:39 +0000497 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000498 __hash_const_iterator(__next_pointer __node) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000499 : __node_(__node)
500 {}
Howard Hinnantb24c8022013-07-23 22:01:58 +0000501#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000502 template <class, class, class, class> friend class __hash_table;
Howard Hinnantf0544c22013-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 Hinnant3e519522010-05-11 19:42:16 +0000506};
507
Howard Hinnant3e519522010-05-11 19:42:16 +0000508template <class _NodePtr>
Howard Hinnantf0544c22013-08-12 18:38:34 +0000509class _LIBCPP_TYPE_VIS_ONLY __hash_local_iterator
Howard Hinnant3e519522010-05-11 19:42:16 +0000510{
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000511 typedef __hash_node_types<_NodePtr> _NodeTypes;
Eric Fiselier40492ba2016-07-23 20:36:55 +0000512 typedef _NodePtr __node_pointer;
513 typedef typename _NodeTypes::__next_pointer __next_pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000514
Eric Fiselier40492ba2016-07-23 20:36:55 +0000515 __next_pointer __node_;
Howard Hinnant3e519522010-05-11 19:42:16 +0000516 size_t __bucket_;
517 size_t __bucket_count_;
518
Howard Hinnant3e519522010-05-11 19:42:16 +0000519public:
520 typedef forward_iterator_tag iterator_category;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000521 typedef typename _NodeTypes::__node_value_type value_type;
522 typedef typename _NodeTypes::difference_type difference_type;
Howard Hinnant3e519522010-05-11 19:42:16 +0000523 typedef value_type& reference;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000524 typedef typename _NodeTypes::__node_value_type_pointer pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000525
Eric Fiselier40492ba2016-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 Hinnantb24c8022013-07-23 22:01:58 +0000528 }
529
530#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant43d99232010-09-21 17:32:39 +0000531 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb24c8022013-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 Hinnant43d99232010-09-21 17:32:39 +0000540 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb24c8022013-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 Hinnantb24c8022013-07-23 22:01:58 +0000558#endif // _LIBCPP_DEBUG_LEVEL >= 2
559
560 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000561 reference operator*() const {
562 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
Howard Hinnantb24c8022013-07-23 22:01:58 +0000563 "Attempted to dereference a non-dereferenceable unordered container local_iterator");
Eric Fiselier40492ba2016-07-23 20:36:55 +0000564 return __node_->__upcast()->__value_;
565 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000566
Howard Hinnant43d99232010-09-21 17:32:39 +0000567 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-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 Hinnantb24c8022013-07-23 22:01:58 +0000577 "Attempted to increment non-incrementable unordered container local_iterator");
Howard Hinnant3e519522010-05-11 19:42:16 +0000578 __node_ = __node_->__next_;
Eric Fiselier40492ba2016-07-23 20:36:55 +0000579 if (__node_ != nullptr && __constrain_hash(__node_->__hash(), __bucket_count_) != __bucket_)
Howard Hinnant3e519522010-05-11 19:42:16 +0000580 __node_ = nullptr;
581 return *this;
582 }
583
Howard Hinnant43d99232010-09-21 17:32:39 +0000584 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-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 Hinnant43d99232010-09-21 17:32:39 +0000592 friend _LIBCPP_INLINE_VISIBILITY
593 bool operator==(const __hash_local_iterator& __x, const __hash_local_iterator& __y)
Howard Hinnantb24c8022013-07-23 22:01:58 +0000594 {
Howard Hinnantb24c8022013-07-23 22:01:58 +0000595 return __x.__node_ == __y.__node_;
596 }
Howard Hinnant43d99232010-09-21 17:32:39 +0000597 friend _LIBCPP_INLINE_VISIBILITY
598 bool operator!=(const __hash_local_iterator& __x, const __hash_local_iterator& __y)
Howard Hinnantb24c8022013-07-23 22:01:58 +0000599 {return !(__x == __y);}
Howard Hinnant3e519522010-05-11 19:42:16 +0000600
601private:
Howard Hinnantb24c8022013-07-23 22:01:58 +0000602#if _LIBCPP_DEBUG_LEVEL >= 2
603 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000604 __hash_local_iterator(__next_pointer __node, size_t __bucket,
Howard Hinnantb24c8022013-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 Hinnant43d99232010-09-21 17:32:39 +0000615 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000616 __hash_local_iterator(__next_pointer __node, size_t __bucket,
Howard Hinnant37141072011-06-04 18:54:24 +0000617 size_t __bucket_count) _NOEXCEPT
Howard Hinnant3e519522010-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 Hinnantb24c8022013-07-23 22:01:58 +0000625#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000626 template <class, class, class, class> friend class __hash_table;
Howard Hinnantf0544c22013-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 Hinnant3e519522010-05-11 19:42:16 +0000629};
630
631template <class _ConstNodePtr>
Howard Hinnantf0544c22013-08-12 18:38:34 +0000632class _LIBCPP_TYPE_VIS_ONLY __hash_const_local_iterator
Howard Hinnant3e519522010-05-11 19:42:16 +0000633{
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000634 typedef __hash_node_types<_ConstNodePtr> _NodeTypes;
Eric Fiselier40492ba2016-07-23 20:36:55 +0000635 typedef _ConstNodePtr __node_pointer;
636 typedef typename _NodeTypes::__next_pointer __next_pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000637
Eric Fiselier40492ba2016-07-23 20:36:55 +0000638 __next_pointer __node_;
Howard Hinnant3e519522010-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 Fiselier934b0922015-12-30 21:52:00 +0000645 typedef typename __rebind_pointer<__node_pointer, __non_const_node>::type
646 __non_const_node_pointer;
Eric Fiselier757373e2016-02-18 00:20:34 +0000647public:
Howard Hinnant3e519522010-05-11 19:42:16 +0000648 typedef __hash_local_iterator<__non_const_node_pointer>
649 __non_const_iterator;
Eric Fiselier757373e2016-02-18 00:20:34 +0000650
Eric Fiselier75d0dcf2016-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 Fiselier934b0922015-12-30 21:52:00 +0000656
Howard Hinnant3e519522010-05-11 19:42:16 +0000657
Eric Fiselier40492ba2016-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 Hinnantb24c8022013-07-23 22:01:58 +0000660 }
661
Howard Hinnant43d99232010-09-21 17:32:39 +0000662 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +0000663 __hash_const_local_iterator(const __non_const_iterator& __x) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000664 : __node_(__x.__node_),
665 __bucket_(__x.__bucket_),
666 __bucket_count_(__x.__bucket_count_)
Howard Hinnantb24c8022013-07-23 22:01:58 +0000667 {
Eric Fiselier40492ba2016-07-23 20:36:55 +0000668 _LIBCPP_DEBUG_MODE(__get_db()->__iterator_copy(this, &__x));
Howard Hinnantb24c8022013-07-23 22:01:58 +0000669 }
670
671#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant43d99232010-09-21 17:32:39 +0000672 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb24c8022013-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 Hinnant43d99232010-09-21 17:32:39 +0000681 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb24c8022013-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 Hinnantb24c8022013-07-23 22:01:58 +0000699#endif // _LIBCPP_DEBUG_LEVEL >= 2
700
701 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000702 reference operator*() const {
703 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
Howard Hinnantb24c8022013-07-23 22:01:58 +0000704 "Attempted to dereference a non-dereferenceable unordered container const_local_iterator");
Eric Fiselier40492ba2016-07-23 20:36:55 +0000705 return __node_->__upcast()->__value_;
706 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000707
Howard Hinnant43d99232010-09-21 17:32:39 +0000708 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-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 Hinnantb24c8022013-07-23 22:01:58 +0000718 "Attempted to increment non-incrementable unordered container const_local_iterator");
Howard Hinnant3e519522010-05-11 19:42:16 +0000719 __node_ = __node_->__next_;
Eric Fiselier40492ba2016-07-23 20:36:55 +0000720 if (__node_ != nullptr && __constrain_hash(__node_->__hash(), __bucket_count_) != __bucket_)
Howard Hinnant3e519522010-05-11 19:42:16 +0000721 __node_ = nullptr;
722 return *this;
723 }
724
Howard Hinnant43d99232010-09-21 17:32:39 +0000725 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-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 Hinnant43d99232010-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 Hinnantb24c8022013-07-23 22:01:58 +0000735 {
Howard Hinnantb24c8022013-07-23 22:01:58 +0000736 return __x.__node_ == __y.__node_;
737 }
Howard Hinnant43d99232010-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 Hinnantb24c8022013-07-23 22:01:58 +0000740 {return !(__x == __y);}
Howard Hinnant3e519522010-05-11 19:42:16 +0000741
742private:
Howard Hinnantb24c8022013-07-23 22:01:58 +0000743#if _LIBCPP_DEBUG_LEVEL >= 2
744 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000745 __hash_const_local_iterator(__next_pointer __node, size_t __bucket,
Howard Hinnantb24c8022013-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 Hinnant43d99232010-09-21 17:32:39 +0000756 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000757 __hash_const_local_iterator(__next_pointer __node, size_t __bucket,
Howard Hinnant37141072011-06-04 18:54:24 +0000758 size_t __bucket_count) _NOEXCEPT
Howard Hinnant3e519522010-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 Hinnantb24c8022013-07-23 22:01:58 +0000766#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000767 template <class, class, class, class> friend class __hash_table;
Howard Hinnantf0544c22013-08-12 18:38:34 +0000768 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_map_const_iterator;
Howard Hinnant3e519522010-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 Hinnant43d99232010-09-21 17:32:39 +0000782 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000783 __bucket_list_deallocator()
Howard Hinnant37141072011-06-04 18:54:24 +0000784 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +0000785 : __data_(0) {}
Howard Hinnant43d99232010-09-21 17:32:39 +0000786
787 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000788 __bucket_list_deallocator(const allocator_type& __a, size_type __size)
Howard Hinnant37141072011-06-04 18:54:24 +0000789 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +0000790 : __data_(__size, __a) {}
791
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000792#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +0000793
Howard Hinnant43d99232010-09-21 17:32:39 +0000794 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000795 __bucket_list_deallocator(__bucket_list_deallocator&& __x)
Howard Hinnant37141072011-06-04 18:54:24 +0000796 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnantce48a112011-06-30 21:18:19 +0000797 : __data_(_VSTD::move(__x.__data_))
Howard Hinnant3e519522010-05-11 19:42:16 +0000798 {
799 __x.size() = 0;
800 }
801
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000802#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +0000803
Howard Hinnant37141072011-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 Hinnant3e519522010-05-11 19:42:16 +0000808
Howard Hinnant43d99232010-09-21 17:32:39 +0000809 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-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 Hinnant3e519522010-05-11 19:42:16 +0000816 {
817 __alloc_traits::deallocate(__alloc(), __p, size());
818 }
819};
820
Howard Hinnantce534202011-06-14 19:58:17 +0000821template <class _Alloc> class __hash_map_node_destructor;
Howard Hinnant3e519522010-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 Fiselier75d0dcf2016-02-10 20:46:23 +0000828
Howard Hinnant3e519522010-05-11 19:42:16 +0000829public:
830 typedef typename __alloc_traits::pointer pointer;
831private:
Eric Fiselierfcd02212016-02-11 11:59:44 +0000832 typedef __hash_node_types<pointer> _NodeTypes;
Howard Hinnant3e519522010-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 Hinnant43d99232010-09-21 17:32:39 +0000841 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf622b582011-07-31 17:04:30 +0000842 explicit __hash_node_destructor(allocator_type& __na,
843 bool __constructed = false) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000844 : __na_(__na),
Howard Hinnantf622b582011-07-31 17:04:30 +0000845 __value_constructed(__constructed)
Howard Hinnant3e519522010-05-11 19:42:16 +0000846 {}
847
Howard Hinnant43d99232010-09-21 17:32:39 +0000848 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +0000849 void operator()(pointer __p) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000850 {
851 if (__value_constructed)
Eric Fiselierfcd02212016-02-11 11:59:44 +0000852 __alloc_traits::destroy(__na_, _NodeTypes::__get_ptr(__p->__value_));
Howard Hinnant3e519522010-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 Fiselier75d0dcf2016-02-10 20:46:23 +0000871 typedef typename
872 __make_hash_node_types<value_type, typename __alloc_traits::void_pointer>::type
873 _NodeTypes;
Howard Hinnant3e519522010-05-11 19:42:16 +0000874public:
Eric Fiselierfcd02212016-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 Smithfde79b42016-03-17 20:45:20 +0000878 typedef typename _NodeTypes::key_type key_type;
Howard Hinnant3e519522010-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 Fiselier75d0dcf2016-02-10 20:46:23 +0000883#ifndef _LIBCPP_ABI_FIX_UNORDERED_CONTAINER_SIZE_TYPE
Howard Hinnant3e519522010-05-11 19:42:16 +0000884 typedef typename __alloc_traits::size_type size_type;
Eric Fiselier75d0dcf2016-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 Hinnant3e519522010-05-11 19:42:16 +0000889public:
890 // Create __node
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000891
892 typedef typename _NodeTypes::__node_type __node;
Marshall Clow1f508012015-04-07 05:21:38 +0000893 typedef typename __rebind_alloc_helper<__alloc_traits, __node>::type __node_allocator;
Howard Hinnant3e519522010-05-11 19:42:16 +0000894 typedef allocator_traits<__node_allocator> __node_traits;
Eric Fiselier8e397682016-02-11 15:22:37 +0000895 typedef typename _NodeTypes::__void_pointer __void_pointer;
Eric Fiselier75d0dcf2016-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 Fiselier40492ba2016-07-23 20:36:55 +0000900 typedef typename _NodeTypes::__next_pointer __next_pointer;
Eric Fiselier75d0dcf2016-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 Hinnant3e519522010-05-11 19:42:16 +0000913
914private:
915
Eric Fiselier40492ba2016-07-23 20:36:55 +0000916 typedef typename __rebind_alloc_helper<__node_traits, __next_pointer>::type __pointer_allocator;
Howard Hinnant3e519522010-05-11 19:42:16 +0000917 typedef __bucket_list_deallocator<__pointer_allocator> __bucket_list_deleter;
Eric Fiselier40492ba2016-07-23 20:36:55 +0000918 typedef unique_ptr<__next_pointer[], __bucket_list_deleter> __bucket_list;
Howard Hinnant3e519522010-05-11 19:42:16 +0000919 typedef allocator_traits<__pointer_allocator> __pointer_alloc_traits;
Eric Fiselier40492ba2016-07-23 20:36:55 +0000920 typedef typename __bucket_list_deleter::pointer __node_pointer_pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000921
922 // --- Member data begin ---
Eric Fiselier75d0dcf2016-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 Hinnant3e519522010-05-11 19:42:16 +0000927 // --- Member data end ---
928
Howard Hinnant37141072011-06-04 18:54:24 +0000929 _LIBCPP_INLINE_VISIBILITY
930 size_type& size() _NOEXCEPT {return __p2_.first();}
Howard Hinnant3e519522010-05-11 19:42:16 +0000931public:
Howard Hinnant37141072011-06-04 18:54:24 +0000932 _LIBCPP_INLINE_VISIBILITY
933 size_type size() const _NOEXCEPT {return __p2_.first();}
Howard Hinnant3e519522010-05-11 19:42:16 +0000934
Howard Hinnant37141072011-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 Hinnant3e519522010-05-11 19:42:16 +0000939
Howard Hinnant37141072011-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 Hinnant3e519522010-05-11 19:42:16 +0000944
Howard Hinnant37141072011-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 Hinnant3e519522010-05-11 19:42:16 +0000949
Howard Hinnant37141072011-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 Hinnant3e519522010-05-11 19:42:16 +0000955
956public:
957 typedef __hash_iterator<__node_pointer> iterator;
Howard Hinnant307f8142013-06-22 15:21:29 +0000958 typedef __hash_const_iterator<__node_pointer> const_iterator;
Howard Hinnant3e519522010-05-11 19:42:16 +0000959 typedef __hash_local_iterator<__node_pointer> local_iterator;
Howard Hinnant307f8142013-06-22 15:21:29 +0000960 typedef __hash_const_local_iterator<__node_pointer> const_local_iterator;
Howard Hinnant3e519522010-05-11 19:42:16 +0000961
Evgeniy Stepanov906c8722015-11-07 01:22:13 +0000962 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-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 Stepanov906c8722015-11-07 01:22:13 +0000970 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-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 Fiselierfcd02212016-02-11 11:59:44 +0000977#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant37141072011-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 Hinnant3e519522010-05-11 19:42:16 +0000985 __hash_table(__hash_table&& __u, const allocator_type& __a);
Eric Fiselierfcd02212016-02-11 11:59:44 +0000986#endif // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +0000987 ~__hash_table();
988
989 __hash_table& operator=(const __hash_table& __u);
Eric Fiselierfcd02212016-02-11 11:59:44 +0000990#ifndef _LIBCPP_CXX03_LANG
Evgeniy Stepanov906c8722015-11-07 01:22:13 +0000991 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-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 Hinnant3e519522010-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 Hinnant43d99232010-09-21 17:32:39 +00001004 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001005 size_type max_size() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001006 {
Eric Fiselier55b31b4e2016-11-23 01:18:56 +00001007 return std::min<size_type>(
1008 allocator_traits<__pointer_allocator>::max_size(
1009 __bucket_list_.get_deleter().__alloc()),
1010 numeric_limits<difference_type >::max()
1011 );
Howard Hinnant3e519522010-05-11 19:42:16 +00001012 }
1013
1014 pair<iterator, bool> __node_insert_unique(__node_pointer __nd);
1015 iterator __node_insert_multi(__node_pointer __nd);
1016 iterator __node_insert_multi(const_iterator __p,
1017 __node_pointer __nd);
1018
Eric Fiselierfcd02212016-02-11 11:59:44 +00001019#ifndef _LIBCPP_CXX03_LANG
1020 template <class _Key, class ..._Args>
Duncan P. N. Exon Smithfde79b42016-03-17 20:45:20 +00001021 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierfcd02212016-02-11 11:59:44 +00001022 pair<iterator, bool> __emplace_unique_key_args(_Key const& __k, _Args&&... __args);
Howard Hinnant3e519522010-05-11 19:42:16 +00001023
Eric Fiselierfcd02212016-02-11 11:59:44 +00001024 template <class... _Args>
Duncan P. N. Exon Smithfde79b42016-03-17 20:45:20 +00001025 _LIBCPP_INLINE_VISIBILITY
1026 pair<iterator, bool> __emplace_unique_impl(_Args&&... __args);
1027
1028 template <class _Pp>
1029 _LIBCPP_INLINE_VISIBILITY
1030 pair<iterator, bool> __emplace_unique(_Pp&& __x) {
1031 return __emplace_unique_extract_key(_VSTD::forward<_Pp>(__x),
1032 __can_extract_key<_Pp, key_type>());
1033 }
Eric Fiselier50088682016-04-16 00:23:12 +00001034
1035 template <class _First, class _Second>
1036 _LIBCPP_INLINE_VISIBILITY
1037 typename enable_if<
1038 __can_extract_map_key<_First, key_type, __container_value_type>::value,
1039 pair<iterator, bool>
1040 >::type __emplace_unique(_First&& __f, _Second&& __s) {
1041 return __emplace_unique_key_args(__f, _VSTD::forward<_First>(__f),
1042 _VSTD::forward<_Second>(__s));
1043 }
1044
Eric Fiselierfcd02212016-02-11 11:59:44 +00001045 template <class... _Args>
Duncan P. N. Exon Smithfde79b42016-03-17 20:45:20 +00001046 _LIBCPP_INLINE_VISIBILITY
1047 pair<iterator, bool> __emplace_unique(_Args&&... __args) {
1048 return __emplace_unique_impl(_VSTD::forward<_Args>(__args)...);
1049 }
1050
1051 template <class _Pp>
1052 _LIBCPP_INLINE_VISIBILITY
1053 pair<iterator, bool>
1054 __emplace_unique_extract_key(_Pp&& __x, __extract_key_fail_tag) {
1055 return __emplace_unique_impl(_VSTD::forward<_Pp>(__x));
1056 }
1057 template <class _Pp>
1058 _LIBCPP_INLINE_VISIBILITY
1059 pair<iterator, bool>
1060 __emplace_unique_extract_key(_Pp&& __x, __extract_key_self_tag) {
1061 return __emplace_unique_key_args(__x, _VSTD::forward<_Pp>(__x));
1062 }
1063 template <class _Pp>
1064 _LIBCPP_INLINE_VISIBILITY
1065 pair<iterator, bool>
1066 __emplace_unique_extract_key(_Pp&& __x, __extract_key_first_tag) {
1067 return __emplace_unique_key_args(__x.first, _VSTD::forward<_Pp>(__x));
1068 }
1069
1070 template <class... _Args>
1071 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierfcd02212016-02-11 11:59:44 +00001072 iterator __emplace_multi(_Args&&... __args);
1073 template <class... _Args>
Duncan P. N. Exon Smithfde79b42016-03-17 20:45:20 +00001074 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierfcd02212016-02-11 11:59:44 +00001075 iterator __emplace_hint_multi(const_iterator __p, _Args&&... __args);
1076
1077
Eric Fiselierde3f2b32015-06-13 07:18:32 +00001078 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierfcd02212016-02-11 11:59:44 +00001079 pair<iterator, bool>
1080 __insert_unique(__container_value_type&& __x) {
1081 return __emplace_unique_key_args(_NodeTypes::__get_key(__x), _VSTD::move(__x));
1082 }
1083
1084 template <class _Pp, class = typename enable_if<
1085 !__is_same_uncvref<_Pp, __container_value_type>::value
1086 >::type>
Eric Fiselierde3f2b32015-06-13 07:18:32 +00001087 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierfcd02212016-02-11 11:59:44 +00001088 pair<iterator, bool> __insert_unique(_Pp&& __x) {
1089 return __emplace_unique(_VSTD::forward<_Pp>(__x));
1090 }
1091
1092 template <class _Pp>
1093 _LIBCPP_INLINE_VISIBILITY
1094 iterator __insert_multi(_Pp&& __x) {
1095 return __emplace_multi(_VSTD::forward<_Pp>(__x));
1096 }
1097
1098 template <class _Pp>
1099 _LIBCPP_INLINE_VISIBILITY
1100 iterator __insert_multi(const_iterator __p, _Pp&& __x) {
1101 return __emplace_hint_multi(__p, _VSTD::forward<_Pp>(__x));
1102 }
1103
1104#else // !defined(_LIBCPP_CXX03_LANG)
1105 template <class _Key, class _Args>
Eric Fiselier4271d012016-09-25 04:05:46 +00001106 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierfcd02212016-02-11 11:59:44 +00001107 pair<iterator, bool> __emplace_unique_key_args(_Key const&, _Args& __args);
1108
1109 iterator __insert_multi(const __container_value_type& __x);
1110 iterator __insert_multi(const_iterator __p, const __container_value_type& __x);
Eric Fiselierde3f2b32015-06-13 07:18:32 +00001111#endif
1112
Eric Fiselierfcd02212016-02-11 11:59:44 +00001113 _LIBCPP_INLINE_VISIBILITY
1114 pair<iterator, bool> __insert_unique(const __container_value_type& __x) {
1115 return __emplace_unique_key_args(_NodeTypes::__get_key(__x), __x);
1116 }
Howard Hinnant3e519522010-05-11 19:42:16 +00001117
Howard Hinnant37141072011-06-04 18:54:24 +00001118 void clear() _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +00001119 void rehash(size_type __n);
Howard Hinnant43d99232010-09-21 17:32:39 +00001120 _LIBCPP_INLINE_VISIBILITY void reserve(size_type __n)
Howard Hinnant3e519522010-05-11 19:42:16 +00001121 {rehash(static_cast<size_type>(ceil(__n / max_load_factor())));}
Howard Hinnant43d99232010-09-21 17:32:39 +00001122
1123 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001124 size_type bucket_count() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001125 {
1126 return __bucket_list_.get_deleter().size();
1127 }
1128
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001129 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001130 iterator begin() _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001131 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001132 iterator end() _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001133 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001134 const_iterator begin() const _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001135 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001136 const_iterator end() const _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +00001137
1138 template <class _Key>
Howard Hinnant43d99232010-09-21 17:32:39 +00001139 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001140 size_type bucket(const _Key& __k) const
Howard Hinnante5c13de2013-07-29 19:05:47 +00001141 {
1142 _LIBCPP_ASSERT(bucket_count() > 0,
1143 "unordered container::bucket(key) called when bucket_count() == 0");
1144 return __constrain_hash(hash_function()(__k), bucket_count());
1145 }
Howard Hinnant3e519522010-05-11 19:42:16 +00001146
1147 template <class _Key>
1148 iterator find(const _Key& __x);
1149 template <class _Key>
1150 const_iterator find(const _Key& __x) const;
1151
Howard Hinnantc003db12011-11-29 18:15:50 +00001152 typedef __hash_node_destructor<__node_allocator> _Dp;
1153 typedef unique_ptr<__node, _Dp> __node_holder;
Howard Hinnant3e519522010-05-11 19:42:16 +00001154
1155 iterator erase(const_iterator __p);
1156 iterator erase(const_iterator __first, const_iterator __last);
1157 template <class _Key>
1158 size_type __erase_unique(const _Key& __k);
1159 template <class _Key>
1160 size_type __erase_multi(const _Key& __k);
Howard Hinnant37141072011-06-04 18:54:24 +00001161 __node_holder remove(const_iterator __p) _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +00001162
1163 template <class _Key>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001164 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001165 size_type __count_unique(const _Key& __k) const;
1166 template <class _Key>
1167 size_type __count_multi(const _Key& __k) const;
1168
1169 template <class _Key>
1170 pair<iterator, iterator>
1171 __equal_range_unique(const _Key& __k);
1172 template <class _Key>
1173 pair<const_iterator, const_iterator>
1174 __equal_range_unique(const _Key& __k) const;
1175
1176 template <class _Key>
1177 pair<iterator, iterator>
1178 __equal_range_multi(const _Key& __k);
1179 template <class _Key>
1180 pair<const_iterator, const_iterator>
1181 __equal_range_multi(const _Key& __k) const;
1182
Howard Hinnant37141072011-06-04 18:54:24 +00001183 void swap(__hash_table& __u)
Eric Fiselier87a82492015-07-18 20:40:46 +00001184#if _LIBCPP_STD_VER <= 11
Howard Hinnant37141072011-06-04 18:54:24 +00001185 _NOEXCEPT_(
Marshall Clowe3fbe142015-07-13 20:04:56 +00001186 __is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value
Marshall Clowe3fbe142015-07-13 20:04:56 +00001187 && (!allocator_traits<__pointer_allocator>::propagate_on_container_swap::value
1188 || __is_nothrow_swappable<__pointer_allocator>::value)
1189 && (!__node_traits::propagate_on_container_swap::value
1190 || __is_nothrow_swappable<__node_allocator>::value)
Marshall Clowe3fbe142015-07-13 20:04:56 +00001191 );
Eric Fiselier87a82492015-07-18 20:40:46 +00001192#else
1193 _NOEXCEPT_(__is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value);
1194#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001195
Howard Hinnant43d99232010-09-21 17:32:39 +00001196 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001197 size_type max_bucket_count() const _NOEXCEPT
Eric Fiselier55b31b4e2016-11-23 01:18:56 +00001198 {return max_size(); }
Howard Hinnant3e519522010-05-11 19:42:16 +00001199 size_type bucket_size(size_type __n) const;
Howard Hinnant37141072011-06-04 18:54:24 +00001200 _LIBCPP_INLINE_VISIBILITY float load_factor() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001201 {
1202 size_type __bc = bucket_count();
1203 return __bc != 0 ? (float)size() / __bc : 0.f;
1204 }
Howard Hinnant37141072011-06-04 18:54:24 +00001205 _LIBCPP_INLINE_VISIBILITY void max_load_factor(float __mlf) _NOEXCEPT
Howard Hinnante5c13de2013-07-29 19:05:47 +00001206 {
1207 _LIBCPP_ASSERT(__mlf > 0,
1208 "unordered container::max_load_factor(lf) called with lf <= 0");
1209 max_load_factor() = _VSTD::max(__mlf, load_factor());
1210 }
Howard Hinnant3e519522010-05-11 19:42:16 +00001211
Howard Hinnantb24c8022013-07-23 22:01:58 +00001212 _LIBCPP_INLINE_VISIBILITY
1213 local_iterator
1214 begin(size_type __n)
1215 {
Howard Hinnante5c13de2013-07-29 19:05:47 +00001216 _LIBCPP_ASSERT(__n < bucket_count(),
1217 "unordered container::begin(n) called with n >= bucket_count()");
Howard Hinnantb24c8022013-07-23 22:01:58 +00001218#if _LIBCPP_DEBUG_LEVEL >= 2
1219 return local_iterator(__bucket_list_[__n], __n, bucket_count(), this);
1220#else
1221 return local_iterator(__bucket_list_[__n], __n, bucket_count());
1222#endif
1223 }
1224
1225 _LIBCPP_INLINE_VISIBILITY
1226 local_iterator
1227 end(size_type __n)
1228 {
Howard Hinnante5c13de2013-07-29 19:05:47 +00001229 _LIBCPP_ASSERT(__n < bucket_count(),
1230 "unordered container::end(n) called with n >= bucket_count()");
Howard Hinnantb24c8022013-07-23 22:01:58 +00001231#if _LIBCPP_DEBUG_LEVEL >= 2
1232 return local_iterator(nullptr, __n, bucket_count(), this);
1233#else
1234 return local_iterator(nullptr, __n, bucket_count());
1235#endif
1236 }
1237
1238 _LIBCPP_INLINE_VISIBILITY
1239 const_local_iterator
1240 cbegin(size_type __n) const
1241 {
Howard Hinnante5c13de2013-07-29 19:05:47 +00001242 _LIBCPP_ASSERT(__n < bucket_count(),
1243 "unordered container::cbegin(n) called with n >= bucket_count()");
Howard Hinnantb24c8022013-07-23 22:01:58 +00001244#if _LIBCPP_DEBUG_LEVEL >= 2
1245 return const_local_iterator(__bucket_list_[__n], __n, bucket_count(), this);
1246#else
1247 return const_local_iterator(__bucket_list_[__n], __n, bucket_count());
1248#endif
1249 }
1250
1251 _LIBCPP_INLINE_VISIBILITY
1252 const_local_iterator
1253 cend(size_type __n) const
1254 {
Howard Hinnante5c13de2013-07-29 19:05:47 +00001255 _LIBCPP_ASSERT(__n < bucket_count(),
1256 "unordered container::cend(n) called with n >= bucket_count()");
Howard Hinnantb24c8022013-07-23 22:01:58 +00001257#if _LIBCPP_DEBUG_LEVEL >= 2
1258 return const_local_iterator(nullptr, __n, bucket_count(), this);
1259#else
1260 return const_local_iterator(nullptr, __n, bucket_count());
1261#endif
1262 }
1263
1264#if _LIBCPP_DEBUG_LEVEL >= 2
1265
1266 bool __dereferenceable(const const_iterator* __i) const;
1267 bool __decrementable(const const_iterator* __i) const;
1268 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1269 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1270
1271#endif // _LIBCPP_DEBUG_LEVEL >= 2
1272
Howard Hinnant3e519522010-05-11 19:42:16 +00001273private:
1274 void __rehash(size_type __n);
1275
Eric Fiselierfcd02212016-02-11 11:59:44 +00001276#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001277 template <class ..._Args>
Eric Fiselierfcd02212016-02-11 11:59:44 +00001278 __node_holder __construct_node(_Args&& ...__args);
1279
1280 template <class _First, class ..._Rest>
1281 __node_holder __construct_node_hash(size_t __hash, _First&& __f, _Rest&&... __rest);
1282#else // _LIBCPP_CXX03_LANG
1283 __node_holder __construct_node(const __container_value_type& __v);
1284 __node_holder __construct_node_hash(size_t __hash, const __container_value_type& __v);
Howard Hinnant3e519522010-05-11 19:42:16 +00001285#endif
Eric Fiselierfcd02212016-02-11 11:59:44 +00001286
Howard Hinnant3e519522010-05-11 19:42:16 +00001287
Howard Hinnant43d99232010-09-21 17:32:39 +00001288 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001289 void __copy_assign_alloc(const __hash_table& __u)
1290 {__copy_assign_alloc(__u, integral_constant<bool,
1291 __node_traits::propagate_on_container_copy_assignment::value>());}
1292 void __copy_assign_alloc(const __hash_table& __u, true_type);
Howard Hinnant43d99232010-09-21 17:32:39 +00001293 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2063662011-12-01 20:21:04 +00001294 void __copy_assign_alloc(const __hash_table&, false_type) {}
Howard Hinnant3e519522010-05-11 19:42:16 +00001295
Eric Fiselierfcd02212016-02-11 11:59:44 +00001296#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001297 void __move_assign(__hash_table& __u, false_type);
Howard Hinnant37141072011-06-04 18:54:24 +00001298 void __move_assign(__hash_table& __u, true_type)
1299 _NOEXCEPT_(
1300 is_nothrow_move_assignable<__node_allocator>::value &&
1301 is_nothrow_move_assignable<hasher>::value &&
1302 is_nothrow_move_assignable<key_equal>::value);
1303 _LIBCPP_INLINE_VISIBILITY
1304 void __move_assign_alloc(__hash_table& __u)
1305 _NOEXCEPT_(
1306 !__node_traits::propagate_on_container_move_assignment::value ||
1307 (is_nothrow_move_assignable<__pointer_allocator>::value &&
1308 is_nothrow_move_assignable<__node_allocator>::value))
Howard Hinnant3e519522010-05-11 19:42:16 +00001309 {__move_assign_alloc(__u, integral_constant<bool,
1310 __node_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnant43d99232010-09-21 17:32:39 +00001311 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001312 void __move_assign_alloc(__hash_table& __u, true_type)
Howard Hinnant37141072011-06-04 18:54:24 +00001313 _NOEXCEPT_(
1314 is_nothrow_move_assignable<__pointer_allocator>::value &&
1315 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +00001316 {
1317 __bucket_list_.get_deleter().__alloc() =
Howard Hinnantce48a112011-06-30 21:18:19 +00001318 _VSTD::move(__u.__bucket_list_.get_deleter().__alloc());
1319 __node_alloc() = _VSTD::move(__u.__node_alloc());
Howard Hinnant3e519522010-05-11 19:42:16 +00001320 }
Howard Hinnant43d99232010-09-21 17:32:39 +00001321 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001322 void __move_assign_alloc(__hash_table&, false_type) _NOEXCEPT {}
Eric Fiselierfcd02212016-02-11 11:59:44 +00001323#endif // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001324
Eric Fiselier40492ba2016-07-23 20:36:55 +00001325 void __deallocate(__next_pointer __np) _NOEXCEPT;
1326 __next_pointer __detach() _NOEXCEPT;
Howard Hinnant307f8142013-06-22 15:21:29 +00001327
Howard Hinnantf0544c22013-08-12 18:38:34 +00001328 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_map;
1329 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_multimap;
Howard Hinnant3e519522010-05-11 19:42:16 +00001330};
1331
1332template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001333inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001334__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table()
Howard Hinnant37141072011-06-04 18:54:24 +00001335 _NOEXCEPT_(
1336 is_nothrow_default_constructible<__bucket_list>::value &&
1337 is_nothrow_default_constructible<__first_node>::value &&
Eric Fiseliere2e332a2015-12-16 00:53:04 +00001338 is_nothrow_default_constructible<__node_allocator>::value &&
Howard Hinnant37141072011-06-04 18:54:24 +00001339 is_nothrow_default_constructible<hasher>::value &&
1340 is_nothrow_default_constructible<key_equal>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +00001341 : __p2_(0),
1342 __p3_(1.0f)
1343{
1344}
1345
1346template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001347inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001348__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const hasher& __hf,
1349 const key_equal& __eql)
1350 : __bucket_list_(nullptr, __bucket_list_deleter()),
1351 __p1_(),
1352 __p2_(0, __hf),
1353 __p3_(1.0f, __eql)
1354{
1355}
1356
1357template <class _Tp, class _Hash, class _Equal, class _Alloc>
1358__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const hasher& __hf,
1359 const key_equal& __eql,
1360 const allocator_type& __a)
1361 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1362 __p1_(__node_allocator(__a)),
1363 __p2_(0, __hf),
1364 __p3_(1.0f, __eql)
1365{
1366}
1367
1368template <class _Tp, class _Hash, class _Equal, class _Alloc>
1369__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const allocator_type& __a)
1370 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1371 __p1_(__node_allocator(__a)),
1372 __p2_(0),
1373 __p3_(1.0f)
1374{
1375}
1376
1377template <class _Tp, class _Hash, class _Equal, class _Alloc>
1378__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const __hash_table& __u)
1379 : __bucket_list_(nullptr,
1380 __bucket_list_deleter(allocator_traits<__pointer_allocator>::
1381 select_on_container_copy_construction(
1382 __u.__bucket_list_.get_deleter().__alloc()), 0)),
1383 __p1_(allocator_traits<__node_allocator>::
1384 select_on_container_copy_construction(__u.__node_alloc())),
1385 __p2_(0, __u.hash_function()),
1386 __p3_(__u.__p3_)
1387{
1388}
1389
1390template <class _Tp, class _Hash, class _Equal, class _Alloc>
1391__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const __hash_table& __u,
1392 const allocator_type& __a)
1393 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1394 __p1_(__node_allocator(__a)),
1395 __p2_(0, __u.hash_function()),
1396 __p3_(__u.__p3_)
1397{
1398}
1399
Eric Fiselierfcd02212016-02-11 11:59:44 +00001400#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001401
1402template <class _Tp, class _Hash, class _Equal, class _Alloc>
1403__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(__hash_table&& __u)
Howard Hinnant37141072011-06-04 18:54:24 +00001404 _NOEXCEPT_(
1405 is_nothrow_move_constructible<__bucket_list>::value &&
1406 is_nothrow_move_constructible<__first_node>::value &&
Eric Fiseliere2e332a2015-12-16 00:53:04 +00001407 is_nothrow_move_constructible<__node_allocator>::value &&
Howard Hinnant37141072011-06-04 18:54:24 +00001408 is_nothrow_move_constructible<hasher>::value &&
1409 is_nothrow_move_constructible<key_equal>::value)
Howard Hinnantce48a112011-06-30 21:18:19 +00001410 : __bucket_list_(_VSTD::move(__u.__bucket_list_)),
1411 __p1_(_VSTD::move(__u.__p1_)),
1412 __p2_(_VSTD::move(__u.__p2_)),
1413 __p3_(_VSTD::move(__u.__p3_))
Howard Hinnant3e519522010-05-11 19:42:16 +00001414{
1415 if (size() > 0)
1416 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001417 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash(), bucket_count())] =
1418 __p1_.first().__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001419 __u.__p1_.first().__next_ = nullptr;
1420 __u.size() = 0;
1421 }
1422}
1423
1424template <class _Tp, class _Hash, class _Equal, class _Alloc>
1425__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(__hash_table&& __u,
1426 const allocator_type& __a)
1427 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1428 __p1_(__node_allocator(__a)),
Howard Hinnantce48a112011-06-30 21:18:19 +00001429 __p2_(0, _VSTD::move(__u.hash_function())),
1430 __p3_(_VSTD::move(__u.__p3_))
Howard Hinnant3e519522010-05-11 19:42:16 +00001431{
1432 if (__a == allocator_type(__u.__node_alloc()))
1433 {
1434 __bucket_list_.reset(__u.__bucket_list_.release());
1435 __bucket_list_.get_deleter().size() = __u.__bucket_list_.get_deleter().size();
1436 __u.__bucket_list_.get_deleter().size() = 0;
1437 if (__u.size() > 0)
1438 {
1439 __p1_.first().__next_ = __u.__p1_.first().__next_;
1440 __u.__p1_.first().__next_ = nullptr;
Eric Fiselier40492ba2016-07-23 20:36:55 +00001441 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash(), bucket_count())] =
1442 __p1_.first().__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001443 size() = __u.size();
1444 __u.size() = 0;
1445 }
1446 }
1447}
1448
Eric Fiselierfcd02212016-02-11 11:59:44 +00001449#endif // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001450
1451template <class _Tp, class _Hash, class _Equal, class _Alloc>
1452__hash_table<_Tp, _Hash, _Equal, _Alloc>::~__hash_table()
1453{
Marshall Clow3b8669e2016-06-30 22:05:45 +00001454 static_assert((is_copy_constructible<key_equal>::value),
1455 "Predicate must be copy-constructible.");
1456 static_assert((is_copy_constructible<hasher>::value),
1457 "Hasher must be copy-constructible.");
Howard Hinnant3e519522010-05-11 19:42:16 +00001458 __deallocate(__p1_.first().__next_);
Howard Hinnantb24c8022013-07-23 22:01:58 +00001459#if _LIBCPP_DEBUG_LEVEL >= 2
1460 __get_db()->__erase_c(this);
1461#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001462}
1463
1464template <class _Tp, class _Hash, class _Equal, class _Alloc>
1465void
1466__hash_table<_Tp, _Hash, _Equal, _Alloc>::__copy_assign_alloc(
1467 const __hash_table& __u, true_type)
1468{
1469 if (__node_alloc() != __u.__node_alloc())
1470 {
1471 clear();
1472 __bucket_list_.reset();
1473 __bucket_list_.get_deleter().size() = 0;
1474 }
1475 __bucket_list_.get_deleter().__alloc() = __u.__bucket_list_.get_deleter().__alloc();
1476 __node_alloc() = __u.__node_alloc();
1477}
1478
1479template <class _Tp, class _Hash, class _Equal, class _Alloc>
1480__hash_table<_Tp, _Hash, _Equal, _Alloc>&
1481__hash_table<_Tp, _Hash, _Equal, _Alloc>::operator=(const __hash_table& __u)
1482{
1483 if (this != &__u)
1484 {
1485 __copy_assign_alloc(__u);
1486 hash_function() = __u.hash_function();
1487 key_eq() = __u.key_eq();
1488 max_load_factor() = __u.max_load_factor();
1489 __assign_multi(__u.begin(), __u.end());
1490 }
1491 return *this;
1492}
1493
1494template <class _Tp, class _Hash, class _Equal, class _Alloc>
1495void
Eric Fiselier40492ba2016-07-23 20:36:55 +00001496__hash_table<_Tp, _Hash, _Equal, _Alloc>::__deallocate(__next_pointer __np)
Howard Hinnant37141072011-06-04 18:54:24 +00001497 _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001498{
1499 __node_allocator& __na = __node_alloc();
1500 while (__np != nullptr)
1501 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001502 __next_pointer __next = __np->__next_;
Howard Hinnantb24c8022013-07-23 22:01:58 +00001503#if _LIBCPP_DEBUG_LEVEL >= 2
1504 __c_node* __c = __get_db()->__find_c_and_lock(this);
1505 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1506 {
1507 --__p;
1508 iterator* __i = static_cast<iterator*>((*__p)->__i_);
1509 if (__i->__node_ == __np)
1510 {
1511 (*__p)->__c_ = nullptr;
1512 if (--__c->end_ != __p)
1513 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1514 }
1515 }
1516 __get_db()->unlock();
1517#endif
Eric Fiselier40492ba2016-07-23 20:36:55 +00001518 __node_pointer __real_np = __np->__upcast();
1519 __node_traits::destroy(__na, _NodeTypes::__get_ptr(__real_np->__value_));
1520 __node_traits::deallocate(__na, __real_np, 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001521 __np = __next;
1522 }
1523}
1524
1525template <class _Tp, class _Hash, class _Equal, class _Alloc>
Eric Fiselier40492ba2016-07-23 20:36:55 +00001526typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__next_pointer
Howard Hinnant37141072011-06-04 18:54:24 +00001527__hash_table<_Tp, _Hash, _Equal, _Alloc>::__detach() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001528{
1529 size_type __bc = bucket_count();
1530 for (size_type __i = 0; __i < __bc; ++__i)
1531 __bucket_list_[__i] = nullptr;
1532 size() = 0;
Eric Fiselier40492ba2016-07-23 20:36:55 +00001533 __next_pointer __cache = __p1_.first().__next_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001534 __p1_.first().__next_ = nullptr;
1535 return __cache;
1536}
1537
Eric Fiselierfcd02212016-02-11 11:59:44 +00001538#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001539
1540template <class _Tp, class _Hash, class _Equal, class _Alloc>
1541void
1542__hash_table<_Tp, _Hash, _Equal, _Alloc>::__move_assign(
1543 __hash_table& __u, true_type)
Howard Hinnant37141072011-06-04 18:54:24 +00001544 _NOEXCEPT_(
1545 is_nothrow_move_assignable<__node_allocator>::value &&
1546 is_nothrow_move_assignable<hasher>::value &&
1547 is_nothrow_move_assignable<key_equal>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +00001548{
1549 clear();
1550 __bucket_list_.reset(__u.__bucket_list_.release());
1551 __bucket_list_.get_deleter().size() = __u.__bucket_list_.get_deleter().size();
1552 __u.__bucket_list_.get_deleter().size() = 0;
1553 __move_assign_alloc(__u);
1554 size() = __u.size();
Howard Hinnantce48a112011-06-30 21:18:19 +00001555 hash_function() = _VSTD::move(__u.hash_function());
Howard Hinnant3e519522010-05-11 19:42:16 +00001556 max_load_factor() = __u.max_load_factor();
Howard Hinnantce48a112011-06-30 21:18:19 +00001557 key_eq() = _VSTD::move(__u.key_eq());
Howard Hinnant3e519522010-05-11 19:42:16 +00001558 __p1_.first().__next_ = __u.__p1_.first().__next_;
1559 if (size() > 0)
1560 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001561 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash(), bucket_count())] =
1562 __p1_.first().__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001563 __u.__p1_.first().__next_ = nullptr;
1564 __u.size() = 0;
1565 }
Howard Hinnantb24c8022013-07-23 22:01:58 +00001566#if _LIBCPP_DEBUG_LEVEL >= 2
1567 __get_db()->swap(this, &__u);
1568#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001569}
1570
1571template <class _Tp, class _Hash, class _Equal, class _Alloc>
1572void
1573__hash_table<_Tp, _Hash, _Equal, _Alloc>::__move_assign(
1574 __hash_table& __u, false_type)
1575{
1576 if (__node_alloc() == __u.__node_alloc())
1577 __move_assign(__u, true_type());
1578 else
1579 {
Howard Hinnantce48a112011-06-30 21:18:19 +00001580 hash_function() = _VSTD::move(__u.hash_function());
1581 key_eq() = _VSTD::move(__u.key_eq());
Howard Hinnant3e519522010-05-11 19:42:16 +00001582 max_load_factor() = __u.max_load_factor();
1583 if (bucket_count() != 0)
1584 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001585 __next_pointer __cache = __detach();
Howard Hinnant3e519522010-05-11 19:42:16 +00001586#ifndef _LIBCPP_NO_EXCEPTIONS
1587 try
1588 {
Howard Hinnantb3371f62010-08-22 00:02:43 +00001589#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001590 const_iterator __i = __u.begin();
1591 while (__cache != nullptr && __u.size() != 0)
1592 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001593 __cache->__upcast()->__value_ =
1594 _VSTD::move(__u.remove(__i++)->__value_);
1595 __next_pointer __next = __cache->__next_;
1596 __node_insert_multi(__cache->__upcast());
Howard Hinnant3e519522010-05-11 19:42:16 +00001597 __cache = __next;
1598 }
1599#ifndef _LIBCPP_NO_EXCEPTIONS
1600 }
1601 catch (...)
1602 {
1603 __deallocate(__cache);
1604 throw;
1605 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00001606#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001607 __deallocate(__cache);
1608 }
1609 const_iterator __i = __u.begin();
1610 while (__u.size() != 0)
1611 {
Eric Fiselierfcd02212016-02-11 11:59:44 +00001612 __node_holder __h = __construct_node(_NodeTypes::__move(__u.remove(__i++)->__value_));
Howard Hinnant3e519522010-05-11 19:42:16 +00001613 __node_insert_multi(__h.get());
1614 __h.release();
1615 }
1616 }
1617}
1618
1619template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001620inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001621__hash_table<_Tp, _Hash, _Equal, _Alloc>&
1622__hash_table<_Tp, _Hash, _Equal, _Alloc>::operator=(__hash_table&& __u)
Howard Hinnant37141072011-06-04 18:54:24 +00001623 _NOEXCEPT_(
1624 __node_traits::propagate_on_container_move_assignment::value &&
1625 is_nothrow_move_assignable<__node_allocator>::value &&
1626 is_nothrow_move_assignable<hasher>::value &&
1627 is_nothrow_move_assignable<key_equal>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +00001628{
1629 __move_assign(__u, integral_constant<bool,
1630 __node_traits::propagate_on_container_move_assignment::value>());
1631 return *this;
1632}
1633
Eric Fiselierfcd02212016-02-11 11:59:44 +00001634#endif // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001635
1636template <class _Tp, class _Hash, class _Equal, class _Alloc>
1637template <class _InputIterator>
1638void
1639__hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_unique(_InputIterator __first,
1640 _InputIterator __last)
1641{
Eric Fiselierfcd02212016-02-11 11:59:44 +00001642 typedef iterator_traits<_InputIterator> _ITraits;
1643 typedef typename _ITraits::value_type _ItValueType;
1644 static_assert((is_same<_ItValueType, __container_value_type>::value),
1645 "__assign_unique may only be called with the containers value type");
1646
Howard Hinnant3e519522010-05-11 19:42:16 +00001647 if (bucket_count() != 0)
1648 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001649 __next_pointer __cache = __detach();
Howard Hinnant3e519522010-05-11 19:42:16 +00001650#ifndef _LIBCPP_NO_EXCEPTIONS
1651 try
1652 {
Howard Hinnantb3371f62010-08-22 00:02:43 +00001653#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001654 for (; __cache != nullptr && __first != __last; ++__first)
1655 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001656 __cache->__upcast()->__value_ = *__first;
1657 __next_pointer __next = __cache->__next_;
1658 __node_insert_unique(__cache->__upcast());
Howard Hinnant3e519522010-05-11 19:42:16 +00001659 __cache = __next;
1660 }
1661#ifndef _LIBCPP_NO_EXCEPTIONS
1662 }
1663 catch (...)
1664 {
1665 __deallocate(__cache);
1666 throw;
1667 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00001668#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001669 __deallocate(__cache);
1670 }
1671 for (; __first != __last; ++__first)
1672 __insert_unique(*__first);
1673}
1674
1675template <class _Tp, class _Hash, class _Equal, class _Alloc>
1676template <class _InputIterator>
1677void
1678__hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_multi(_InputIterator __first,
1679 _InputIterator __last)
1680{
Eric Fiselierfcd02212016-02-11 11:59:44 +00001681 typedef iterator_traits<_InputIterator> _ITraits;
1682 typedef typename _ITraits::value_type _ItValueType;
1683 static_assert((is_same<_ItValueType, __container_value_type>::value ||
1684 is_same<_ItValueType, __node_value_type>::value),
1685 "__assign_multi may only be called with the containers value type"
1686 " or the nodes value type");
Howard Hinnant3e519522010-05-11 19:42:16 +00001687 if (bucket_count() != 0)
1688 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001689 __next_pointer __cache = __detach();
Howard Hinnant3e519522010-05-11 19:42:16 +00001690#ifndef _LIBCPP_NO_EXCEPTIONS
1691 try
1692 {
Howard Hinnantb3371f62010-08-22 00:02:43 +00001693#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001694 for (; __cache != nullptr && __first != __last; ++__first)
1695 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001696 __cache->__upcast()->__value_ = *__first;
1697 __next_pointer __next = __cache->__next_;
1698 __node_insert_multi(__cache->__upcast());
Howard Hinnant3e519522010-05-11 19:42:16 +00001699 __cache = __next;
1700 }
1701#ifndef _LIBCPP_NO_EXCEPTIONS
1702 }
1703 catch (...)
1704 {
1705 __deallocate(__cache);
1706 throw;
1707 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00001708#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001709 __deallocate(__cache);
1710 }
1711 for (; __first != __last; ++__first)
Eric Fiselierfcd02212016-02-11 11:59:44 +00001712 __insert_multi(_NodeTypes::__get_value(*__first));
Howard Hinnant3e519522010-05-11 19:42:16 +00001713}
1714
1715template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001716inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001717typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
Howard Hinnant37141072011-06-04 18:54:24 +00001718__hash_table<_Tp, _Hash, _Equal, _Alloc>::begin() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001719{
Howard Hinnantb24c8022013-07-23 22:01:58 +00001720#if _LIBCPP_DEBUG_LEVEL >= 2
1721 return iterator(__p1_.first().__next_, this);
1722#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001723 return iterator(__p1_.first().__next_);
Howard Hinnantb24c8022013-07-23 22:01:58 +00001724#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001725}
1726
1727template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001728inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001729typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
Howard Hinnant37141072011-06-04 18:54:24 +00001730__hash_table<_Tp, _Hash, _Equal, _Alloc>::end() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001731{
Howard Hinnantb24c8022013-07-23 22:01:58 +00001732#if _LIBCPP_DEBUG_LEVEL >= 2
1733 return iterator(nullptr, this);
1734#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001735 return iterator(nullptr);
Howard Hinnantb24c8022013-07-23 22:01:58 +00001736#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001737}
1738
1739template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001740inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001741typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
Howard Hinnant37141072011-06-04 18:54:24 +00001742__hash_table<_Tp, _Hash, _Equal, _Alloc>::begin() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001743{
Howard Hinnantb24c8022013-07-23 22:01:58 +00001744#if _LIBCPP_DEBUG_LEVEL >= 2
1745 return const_iterator(__p1_.first().__next_, this);
1746#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001747 return const_iterator(__p1_.first().__next_);
Howard Hinnantb24c8022013-07-23 22:01:58 +00001748#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001749}
1750
1751template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001752inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001753typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
Howard Hinnant37141072011-06-04 18:54:24 +00001754__hash_table<_Tp, _Hash, _Equal, _Alloc>::end() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001755{
Howard Hinnantb24c8022013-07-23 22:01:58 +00001756#if _LIBCPP_DEBUG_LEVEL >= 2
1757 return const_iterator(nullptr, this);
1758#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001759 return const_iterator(nullptr);
Howard Hinnantb24c8022013-07-23 22:01:58 +00001760#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001761}
1762
1763template <class _Tp, class _Hash, class _Equal, class _Alloc>
1764void
Howard Hinnant37141072011-06-04 18:54:24 +00001765__hash_table<_Tp, _Hash, _Equal, _Alloc>::clear() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001766{
1767 if (size() > 0)
1768 {
1769 __deallocate(__p1_.first().__next_);
1770 __p1_.first().__next_ = nullptr;
1771 size_type __bc = bucket_count();
Howard Hinnant1f8da842011-07-05 14:14:17 +00001772 for (size_type __i = 0; __i < __bc; ++__i)
Howard Hinnant3e519522010-05-11 19:42:16 +00001773 __bucket_list_[__i] = nullptr;
1774 size() = 0;
1775 }
1776}
1777
1778template <class _Tp, class _Hash, class _Equal, class _Alloc>
1779pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
1780__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_unique(__node_pointer __nd)
1781{
1782 __nd->__hash_ = hash_function()(__nd->__value_);
1783 size_type __bc = bucket_count();
1784 bool __inserted = false;
Eric Fiselier40492ba2016-07-23 20:36:55 +00001785 __next_pointer __ndptr;
Howard Hinnant3e519522010-05-11 19:42:16 +00001786 size_t __chash;
1787 if (__bc != 0)
1788 {
Howard Hinnant4cb38a82012-07-06 17:31:14 +00001789 __chash = __constrain_hash(__nd->__hash_, __bc);
Howard Hinnant3e519522010-05-11 19:42:16 +00001790 __ndptr = __bucket_list_[__chash];
1791 if (__ndptr != nullptr)
1792 {
1793 for (__ndptr = __ndptr->__next_; __ndptr != nullptr &&
Eric Fiselier40492ba2016-07-23 20:36:55 +00001794 __constrain_hash(__ndptr->__hash(), __bc) == __chash;
Howard Hinnant3e519522010-05-11 19:42:16 +00001795 __ndptr = __ndptr->__next_)
1796 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001797 if (key_eq()(__ndptr->__upcast()->__value_, __nd->__value_))
Howard Hinnant3e519522010-05-11 19:42:16 +00001798 goto __done;
1799 }
1800 }
1801 }
1802 {
1803 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1804 {
Eric Fiselier9ba5c112015-02-02 21:31:48 +00001805 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnant3e519522010-05-11 19:42:16 +00001806 size_type(ceil(float(size() + 1) / max_load_factor()))));
1807 __bc = bucket_count();
Howard Hinnant4cb38a82012-07-06 17:31:14 +00001808 __chash = __constrain_hash(__nd->__hash_, __bc);
Howard Hinnant3e519522010-05-11 19:42:16 +00001809 }
1810 // insert_after __bucket_list_[__chash], or __first_node if bucket is null
Eric Fiselier40492ba2016-07-23 20:36:55 +00001811 __next_pointer __pn = __bucket_list_[__chash];
Howard Hinnant3e519522010-05-11 19:42:16 +00001812 if (__pn == nullptr)
1813 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001814 __pn =__p1_.first().__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001815 __nd->__next_ = __pn->__next_;
Eric Fiselier40492ba2016-07-23 20:36:55 +00001816 __pn->__next_ = __nd->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001817 // fix up __bucket_list_
1818 __bucket_list_[__chash] = __pn;
1819 if (__nd->__next_ != nullptr)
Eric Fiselier40492ba2016-07-23 20:36:55 +00001820 __bucket_list_[__constrain_hash(__nd->__next_->__hash(), __bc)] = __nd->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001821 }
1822 else
1823 {
1824 __nd->__next_ = __pn->__next_;
Eric Fiselier40492ba2016-07-23 20:36:55 +00001825 __pn->__next_ = __nd->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001826 }
Eric Fiselier40492ba2016-07-23 20:36:55 +00001827 __ndptr = __nd->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001828 // increment size
1829 ++size();
1830 __inserted = true;
1831 }
1832__done:
Howard Hinnantb24c8022013-07-23 22:01:58 +00001833#if _LIBCPP_DEBUG_LEVEL >= 2
1834 return pair<iterator, bool>(iterator(__ndptr, this), __inserted);
1835#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001836 return pair<iterator, bool>(iterator(__ndptr), __inserted);
Howard Hinnantb24c8022013-07-23 22:01:58 +00001837#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001838}
1839
1840template <class _Tp, class _Hash, class _Equal, class _Alloc>
1841typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1842__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi(__node_pointer __cp)
1843{
1844 __cp->__hash_ = hash_function()(__cp->__value_);
1845 size_type __bc = bucket_count();
1846 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1847 {
Eric Fiselier9ba5c112015-02-02 21:31:48 +00001848 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnant3e519522010-05-11 19:42:16 +00001849 size_type(ceil(float(size() + 1) / max_load_factor()))));
1850 __bc = bucket_count();
1851 }
Howard Hinnant4cb38a82012-07-06 17:31:14 +00001852 size_t __chash = __constrain_hash(__cp->__hash_, __bc);
Eric Fiselier40492ba2016-07-23 20:36:55 +00001853 __next_pointer __pn = __bucket_list_[__chash];
Howard Hinnant3e519522010-05-11 19:42:16 +00001854 if (__pn == nullptr)
1855 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001856 __pn =__p1_.first().__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001857 __cp->__next_ = __pn->__next_;
Eric Fiselier40492ba2016-07-23 20:36:55 +00001858 __pn->__next_ = __cp->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001859 // fix up __bucket_list_
1860 __bucket_list_[__chash] = __pn;
1861 if (__cp->__next_ != nullptr)
Eric Fiselier40492ba2016-07-23 20:36:55 +00001862 __bucket_list_[__constrain_hash(__cp->__next_->__hash(), __bc)]
1863 = __cp->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001864 }
1865 else
1866 {
1867 for (bool __found = false; __pn->__next_ != nullptr &&
Eric Fiselier40492ba2016-07-23 20:36:55 +00001868 __constrain_hash(__pn->__next_->__hash(), __bc) == __chash;
Howard Hinnant3e519522010-05-11 19:42:16 +00001869 __pn = __pn->__next_)
Howard Hinnantb3371f62010-08-22 00:02:43 +00001870 {
Howard Hinnant3e519522010-05-11 19:42:16 +00001871 // __found key_eq() action
1872 // false false loop
1873 // true true loop
1874 // false true set __found to true
1875 // true false break
Eric Fiselier40492ba2016-07-23 20:36:55 +00001876 if (__found != (__pn->__next_->__hash() == __cp->__hash_ &&
1877 key_eq()(__pn->__next_->__upcast()->__value_, __cp->__value_)))
Howard Hinnant3e519522010-05-11 19:42:16 +00001878 {
1879 if (!__found)
1880 __found = true;
1881 else
1882 break;
1883 }
1884 }
1885 __cp->__next_ = __pn->__next_;
Eric Fiselier40492ba2016-07-23 20:36:55 +00001886 __pn->__next_ = __cp->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001887 if (__cp->__next_ != nullptr)
1888 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001889 size_t __nhash = __constrain_hash(__cp->__next_->__hash(), __bc);
Howard Hinnant3e519522010-05-11 19:42:16 +00001890 if (__nhash != __chash)
Eric Fiselier40492ba2016-07-23 20:36:55 +00001891 __bucket_list_[__nhash] = __cp->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001892 }
1893 }
1894 ++size();
Howard Hinnantb24c8022013-07-23 22:01:58 +00001895#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselier40492ba2016-07-23 20:36:55 +00001896 return iterator(__cp->__ptr(), this);
Howard Hinnantb24c8022013-07-23 22:01:58 +00001897#else
Eric Fiselier40492ba2016-07-23 20:36:55 +00001898 return iterator(__cp->__ptr());
Howard Hinnantb24c8022013-07-23 22:01:58 +00001899#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001900}
1901
1902template <class _Tp, class _Hash, class _Equal, class _Alloc>
1903typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1904__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi(
1905 const_iterator __p, __node_pointer __cp)
1906{
Howard Hinnant2f51de52013-08-02 17:50:49 +00001907#if _LIBCPP_DEBUG_LEVEL >= 2
1908 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1909 "unordered container::emplace_hint(const_iterator, args...) called with an iterator not"
1910 " referring to this unordered container");
1911#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001912 if (__p != end() && key_eq()(*__p, __cp->__value_))
1913 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001914 __next_pointer __np = __p.__node_;
1915 __cp->__hash_ = __np->__hash();
Howard Hinnant3e519522010-05-11 19:42:16 +00001916 size_type __bc = bucket_count();
1917 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1918 {
Eric Fiselier9ba5c112015-02-02 21:31:48 +00001919 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnant3e519522010-05-11 19:42:16 +00001920 size_type(ceil(float(size() + 1) / max_load_factor()))));
1921 __bc = bucket_count();
1922 }
Howard Hinnant4cb38a82012-07-06 17:31:14 +00001923 size_t __chash = __constrain_hash(__cp->__hash_, __bc);
Eric Fiselier40492ba2016-07-23 20:36:55 +00001924 __next_pointer __pp = __bucket_list_[__chash];
Howard Hinnant3e519522010-05-11 19:42:16 +00001925 while (__pp->__next_ != __np)
1926 __pp = __pp->__next_;
1927 __cp->__next_ = __np;
Eric Fiselier40492ba2016-07-23 20:36:55 +00001928 __pp->__next_ = static_cast<__next_pointer>(__cp);
Howard Hinnant3e519522010-05-11 19:42:16 +00001929 ++size();
Howard Hinnantb24c8022013-07-23 22:01:58 +00001930#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselier40492ba2016-07-23 20:36:55 +00001931 return iterator(static_cast<__next_pointer>(__cp), this);
Howard Hinnantb24c8022013-07-23 22:01:58 +00001932#else
Eric Fiselier40492ba2016-07-23 20:36:55 +00001933 return iterator(static_cast<__next_pointer>(__cp));
Howard Hinnantb24c8022013-07-23 22:01:58 +00001934#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001935 }
1936 return __node_insert_multi(__cp);
1937}
1938
Eric Fiselierde3f2b32015-06-13 07:18:32 +00001939
1940
Eric Fiselierfcd02212016-02-11 11:59:44 +00001941#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierde3f2b32015-06-13 07:18:32 +00001942template <class _Tp, class _Hash, class _Equal, class _Alloc>
Eric Fiselierfcd02212016-02-11 11:59:44 +00001943template <class _Key, class ..._Args>
Eric Fiselierde3f2b32015-06-13 07:18:32 +00001944pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
Eric Fiselierfcd02212016-02-11 11:59:44 +00001945__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_key_args(_Key const& __k, _Args&&... __args)
Eric Fiselierde3f2b32015-06-13 07:18:32 +00001946#else
1947template <class _Tp, class _Hash, class _Equal, class _Alloc>
Eric Fiselierfcd02212016-02-11 11:59:44 +00001948template <class _Key, class _Args>
Eric Fiselierde3f2b32015-06-13 07:18:32 +00001949pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
Eric Fiselierfcd02212016-02-11 11:59:44 +00001950__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_key_args(_Key const& __k, _Args& __args)
Eric Fiselierde3f2b32015-06-13 07:18:32 +00001951#endif
1952{
Eric Fiselierfcd02212016-02-11 11:59:44 +00001953
1954 size_t __hash = hash_function()(__k);
Howard Hinnant3e519522010-05-11 19:42:16 +00001955 size_type __bc = bucket_count();
1956 bool __inserted = false;
Eric Fiselier40492ba2016-07-23 20:36:55 +00001957 __next_pointer __nd;
Howard Hinnant3e519522010-05-11 19:42:16 +00001958 size_t __chash;
1959 if (__bc != 0)
1960 {
Howard Hinnant4cb38a82012-07-06 17:31:14 +00001961 __chash = __constrain_hash(__hash, __bc);
Howard Hinnant3e519522010-05-11 19:42:16 +00001962 __nd = __bucket_list_[__chash];
1963 if (__nd != nullptr)
1964 {
1965 for (__nd = __nd->__next_; __nd != nullptr &&
Eric Fiselier1a06fe52016-07-24 06:22:25 +00001966 (__nd->__hash() == __hash || __constrain_hash(__nd->__hash(), __bc) == __chash);
Howard Hinnant3e519522010-05-11 19:42:16 +00001967 __nd = __nd->__next_)
1968 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001969 if (key_eq()(__nd->__upcast()->__value_, __k))
Howard Hinnant3e519522010-05-11 19:42:16 +00001970 goto __done;
1971 }
1972 }
1973 }
1974 {
Eric Fiselierfcd02212016-02-11 11:59:44 +00001975#ifndef _LIBCPP_CXX03_LANG
1976 __node_holder __h = __construct_node_hash(__hash, _VSTD::forward<_Args>(__args)...);
1977#else
1978 __node_holder __h = __construct_node_hash(__hash, __args);
1979#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001980 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1981 {
Eric Fiselier9ba5c112015-02-02 21:31:48 +00001982 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnant3e519522010-05-11 19:42:16 +00001983 size_type(ceil(float(size() + 1) / max_load_factor()))));
1984 __bc = bucket_count();
Howard Hinnant4cb38a82012-07-06 17:31:14 +00001985 __chash = __constrain_hash(__hash, __bc);
Howard Hinnant3e519522010-05-11 19:42:16 +00001986 }
1987 // insert_after __bucket_list_[__chash], or __first_node if bucket is null
Eric Fiselier40492ba2016-07-23 20:36:55 +00001988 __next_pointer __pn = __bucket_list_[__chash];
Howard Hinnant3e519522010-05-11 19:42:16 +00001989 if (__pn == nullptr)
1990 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001991 __pn = __p1_.first().__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001992 __h->__next_ = __pn->__next_;
Eric Fiselier40492ba2016-07-23 20:36:55 +00001993 __pn->__next_ = __h.get()->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001994 // fix up __bucket_list_
1995 __bucket_list_[__chash] = __pn;
1996 if (__h->__next_ != nullptr)
Eric Fiselier40492ba2016-07-23 20:36:55 +00001997 __bucket_list_[__constrain_hash(__h->__next_->__hash(), __bc)]
1998 = __h.get()->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001999 }
2000 else
2001 {
2002 __h->__next_ = __pn->__next_;
Eric Fiselier40492ba2016-07-23 20:36:55 +00002003 __pn->__next_ = static_cast<__next_pointer>(__h.get());
Howard Hinnant3e519522010-05-11 19:42:16 +00002004 }
Eric Fiselier40492ba2016-07-23 20:36:55 +00002005 __nd = static_cast<__next_pointer>(__h.release());
Howard Hinnant3e519522010-05-11 19:42:16 +00002006 // increment size
2007 ++size();
2008 __inserted = true;
2009 }
2010__done:
Howard Hinnantb24c8022013-07-23 22:01:58 +00002011#if _LIBCPP_DEBUG_LEVEL >= 2
2012 return pair<iterator, bool>(iterator(__nd, this), __inserted);
2013#else
Howard Hinnant3e519522010-05-11 19:42:16 +00002014 return pair<iterator, bool>(iterator(__nd), __inserted);
Howard Hinnantb24c8022013-07-23 22:01:58 +00002015#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002016}
2017
Eric Fiselierfcd02212016-02-11 11:59:44 +00002018#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00002019
2020template <class _Tp, class _Hash, class _Equal, class _Alloc>
2021template <class... _Args>
2022pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
Duncan P. N. Exon Smithfde79b42016-03-17 20:45:20 +00002023__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_impl(_Args&&... __args)
Howard Hinnant3e519522010-05-11 19:42:16 +00002024{
Howard Hinnantce48a112011-06-30 21:18:19 +00002025 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnant3e519522010-05-11 19:42:16 +00002026 pair<iterator, bool> __r = __node_insert_unique(__h.get());
2027 if (__r.second)
2028 __h.release();
2029 return __r;
2030}
2031
2032template <class _Tp, class _Hash, class _Equal, class _Alloc>
2033template <class... _Args>
2034typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2035__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_multi(_Args&&... __args)
2036{
Howard Hinnantce48a112011-06-30 21:18:19 +00002037 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnant3e519522010-05-11 19:42:16 +00002038 iterator __r = __node_insert_multi(__h.get());
2039 __h.release();
2040 return __r;
2041}
2042
2043template <class _Tp, class _Hash, class _Equal, class _Alloc>
2044template <class... _Args>
2045typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2046__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_hint_multi(
2047 const_iterator __p, _Args&&... __args)
2048{
Howard Hinnante5c13de2013-07-29 19:05:47 +00002049#if _LIBCPP_DEBUG_LEVEL >= 2
2050 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2051 "unordered container::emplace_hint(const_iterator, args...) called with an iterator not"
2052 " referring to this unordered container");
2053#endif
Howard Hinnantce48a112011-06-30 21:18:19 +00002054 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnant3e519522010-05-11 19:42:16 +00002055 iterator __r = __node_insert_multi(__p, __h.get());
2056 __h.release();
2057 return __r;
2058}
2059
Eric Fiselierfcd02212016-02-11 11:59:44 +00002060#else // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00002061
2062template <class _Tp, class _Hash, class _Equal, class _Alloc>
2063typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
Eric Fiselierfcd02212016-02-11 11:59:44 +00002064__hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const __container_value_type& __x)
Howard Hinnant3e519522010-05-11 19:42:16 +00002065{
2066 __node_holder __h = __construct_node(__x);
2067 iterator __r = __node_insert_multi(__h.get());
2068 __h.release();
2069 return __r;
2070}
2071
2072template <class _Tp, class _Hash, class _Equal, class _Alloc>
2073typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2074__hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const_iterator __p,
Eric Fiselierfcd02212016-02-11 11:59:44 +00002075 const __container_value_type& __x)
Howard Hinnant3e519522010-05-11 19:42:16 +00002076{
Howard Hinnante5c13de2013-07-29 19:05:47 +00002077#if _LIBCPP_DEBUG_LEVEL >= 2
2078 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2079 "unordered container::insert(const_iterator, lvalue) called with an iterator not"
2080 " referring to this unordered container");
2081#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002082 __node_holder __h = __construct_node(__x);
2083 iterator __r = __node_insert_multi(__p, __h.get());
2084 __h.release();
2085 return __r;
2086}
2087
Eric Fiselierfcd02212016-02-11 11:59:44 +00002088#endif // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00002089
2090template <class _Tp, class _Hash, class _Equal, class _Alloc>
2091void
2092__hash_table<_Tp, _Hash, _Equal, _Alloc>::rehash(size_type __n)
2093{
Howard Hinnant4cb38a82012-07-06 17:31:14 +00002094 if (__n == 1)
2095 __n = 2;
2096 else if (__n & (__n - 1))
2097 __n = __next_prime(__n);
Howard Hinnant3e519522010-05-11 19:42:16 +00002098 size_type __bc = bucket_count();
2099 if (__n > __bc)
2100 __rehash(__n);
Howard Hinnant4cb38a82012-07-06 17:31:14 +00002101 else if (__n < __bc)
Howard Hinnant3e519522010-05-11 19:42:16 +00002102 {
Howard Hinnantce48a112011-06-30 21:18:19 +00002103 __n = _VSTD::max<size_type>
Howard Hinnant3e519522010-05-11 19:42:16 +00002104 (
2105 __n,
Eric Fiselier9ba5c112015-02-02 21:31:48 +00002106 __is_hash_power2(__bc) ? __next_hash_pow2(size_t(ceil(float(size()) / max_load_factor()))) :
2107 __next_prime(size_t(ceil(float(size()) / max_load_factor())))
Howard Hinnant3e519522010-05-11 19:42:16 +00002108 );
2109 if (__n < __bc)
2110 __rehash(__n);
2111 }
2112}
2113
2114template <class _Tp, class _Hash, class _Equal, class _Alloc>
2115void
2116__hash_table<_Tp, _Hash, _Equal, _Alloc>::__rehash(size_type __nbc)
2117{
Howard Hinnantb24c8022013-07-23 22:01:58 +00002118#if _LIBCPP_DEBUG_LEVEL >= 2
2119 __get_db()->__invalidate_all(this);
2120#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant3e519522010-05-11 19:42:16 +00002121 __pointer_allocator& __npa = __bucket_list_.get_deleter().__alloc();
2122 __bucket_list_.reset(__nbc > 0 ?
2123 __pointer_alloc_traits::allocate(__npa, __nbc) : nullptr);
2124 __bucket_list_.get_deleter().size() = __nbc;
2125 if (__nbc > 0)
2126 {
2127 for (size_type __i = 0; __i < __nbc; ++__i)
2128 __bucket_list_[__i] = nullptr;
Eric Fiselier40492ba2016-07-23 20:36:55 +00002129 __next_pointer __pp = __p1_.first().__ptr();
2130 __next_pointer __cp = __pp->__next_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002131 if (__cp != nullptr)
2132 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00002133 size_type __chash = __constrain_hash(__cp->__hash(), __nbc);
Howard Hinnant3e519522010-05-11 19:42:16 +00002134 __bucket_list_[__chash] = __pp;
2135 size_type __phash = __chash;
2136 for (__pp = __cp, __cp = __cp->__next_; __cp != nullptr;
2137 __cp = __pp->__next_)
2138 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00002139 __chash = __constrain_hash(__cp->__hash(), __nbc);
Howard Hinnant3e519522010-05-11 19:42:16 +00002140 if (__chash == __phash)
2141 __pp = __cp;
2142 else
2143 {
2144 if (__bucket_list_[__chash] == nullptr)
2145 {
2146 __bucket_list_[__chash] = __pp;
2147 __pp = __cp;
2148 __phash = __chash;
2149 }
2150 else
2151 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00002152 __next_pointer __np = __cp;
Howard Hinnant3e519522010-05-11 19:42:16 +00002153 for (; __np->__next_ != nullptr &&
Eric Fiselier40492ba2016-07-23 20:36:55 +00002154 key_eq()(__cp->__upcast()->__value_,
2155 __np->__next_->__upcast()->__value_);
Howard Hinnant3e519522010-05-11 19:42:16 +00002156 __np = __np->__next_)
2157 ;
2158 __pp->__next_ = __np->__next_;
2159 __np->__next_ = __bucket_list_[__chash]->__next_;
2160 __bucket_list_[__chash]->__next_ = __cp;
Howard Hinnantb3371f62010-08-22 00:02:43 +00002161
Howard Hinnant3e519522010-05-11 19:42:16 +00002162 }
2163 }
2164 }
2165 }
2166 }
2167}
2168
2169template <class _Tp, class _Hash, class _Equal, class _Alloc>
2170template <class _Key>
2171typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2172__hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k)
2173{
2174 size_t __hash = hash_function()(__k);
2175 size_type __bc = bucket_count();
2176 if (__bc != 0)
2177 {
Howard Hinnant4cb38a82012-07-06 17:31:14 +00002178 size_t __chash = __constrain_hash(__hash, __bc);
Eric Fiselier40492ba2016-07-23 20:36:55 +00002179 __next_pointer __nd = __bucket_list_[__chash];
Howard Hinnant3e519522010-05-11 19:42:16 +00002180 if (__nd != nullptr)
2181 {
2182 for (__nd = __nd->__next_; __nd != nullptr &&
Eric Fiselier40492ba2016-07-23 20:36:55 +00002183 (__nd->__hash() == __hash
2184 || __constrain_hash(__nd->__hash(), __bc) == __chash);
Howard Hinnant3e519522010-05-11 19:42:16 +00002185 __nd = __nd->__next_)
2186 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00002187 if ((__nd->__hash() == __hash)
2188 && key_eq()(__nd->__upcast()->__value_, __k))
Howard Hinnantb24c8022013-07-23 22:01:58 +00002189#if _LIBCPP_DEBUG_LEVEL >= 2
2190 return iterator(__nd, this);
2191#else
Howard Hinnant3e519522010-05-11 19:42:16 +00002192 return iterator(__nd);
Howard Hinnantb24c8022013-07-23 22:01:58 +00002193#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002194 }
2195 }
2196 }
2197 return end();
2198}
2199
2200template <class _Tp, class _Hash, class _Equal, class _Alloc>
2201template <class _Key>
2202typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
2203__hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k) const
2204{
2205 size_t __hash = hash_function()(__k);
2206 size_type __bc = bucket_count();
2207 if (__bc != 0)
2208 {
Howard Hinnant4cb38a82012-07-06 17:31:14 +00002209 size_t __chash = __constrain_hash(__hash, __bc);
Eric Fiselier40492ba2016-07-23 20:36:55 +00002210 __next_pointer __nd = __bucket_list_[__chash];
Howard Hinnant3e519522010-05-11 19:42:16 +00002211 if (__nd != nullptr)
2212 {
2213 for (__nd = __nd->__next_; __nd != nullptr &&
Eric Fiselier40492ba2016-07-23 20:36:55 +00002214 (__hash == __nd->__hash()
2215 || __constrain_hash(__nd->__hash(), __bc) == __chash);
Howard Hinnant3e519522010-05-11 19:42:16 +00002216 __nd = __nd->__next_)
2217 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00002218 if ((__nd->__hash() == __hash)
2219 && key_eq()(__nd->__upcast()->__value_, __k))
Howard Hinnantb24c8022013-07-23 22:01:58 +00002220#if _LIBCPP_DEBUG_LEVEL >= 2
2221 return const_iterator(__nd, this);
2222#else
Howard Hinnant3e519522010-05-11 19:42:16 +00002223 return const_iterator(__nd);
Howard Hinnantb24c8022013-07-23 22:01:58 +00002224#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002225 }
2226 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00002227
Howard Hinnant3e519522010-05-11 19:42:16 +00002228 }
2229 return end();
2230}
2231
Eric Fiselierfcd02212016-02-11 11:59:44 +00002232#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00002233
2234template <class _Tp, class _Hash, class _Equal, class _Alloc>
2235template <class ..._Args>
2236typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
2237__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(_Args&& ...__args)
2238{
Eric Fiselierfcd02212016-02-11 11:59:44 +00002239 static_assert(!__is_hash_value_type<_Args...>::value,
2240 "Construct cannot be called with a hash value type");
Howard Hinnant3e519522010-05-11 19:42:16 +00002241 __node_allocator& __na = __node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00002242 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselierfcd02212016-02-11 11:59:44 +00002243 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), _VSTD::forward<_Args>(__args)...);
Howard Hinnant3e519522010-05-11 19:42:16 +00002244 __h.get_deleter().__value_constructed = true;
2245 __h->__hash_ = hash_function()(__h->__value_);
2246 __h->__next_ = nullptr;
2247 return __h;
2248}
2249
2250template <class _Tp, class _Hash, class _Equal, class _Alloc>
Eric Fiselierfcd02212016-02-11 11:59:44 +00002251template <class _First, class ..._Rest>
Howard Hinnant3e519522010-05-11 19:42:16 +00002252typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Eric Fiselierfcd02212016-02-11 11:59:44 +00002253__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node_hash(
2254 size_t __hash, _First&& __f, _Rest&& ...__rest)
Howard Hinnant3e519522010-05-11 19:42:16 +00002255{
Eric Fiselierfcd02212016-02-11 11:59:44 +00002256 static_assert(!__is_hash_value_type<_First, _Rest...>::value,
2257 "Construct cannot be called with a hash value type");
Howard Hinnant3e519522010-05-11 19:42:16 +00002258 __node_allocator& __na = __node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00002259 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselierfcd02212016-02-11 11:59:44 +00002260 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_),
2261 _VSTD::forward<_First>(__f),
2262 _VSTD::forward<_Rest>(__rest)...);
Howard Hinnant3e519522010-05-11 19:42:16 +00002263 __h.get_deleter().__value_constructed = true;
2264 __h->__hash_ = __hash;
2265 __h->__next_ = nullptr;
Howard Hinnant179b1f82013-08-22 18:29:50 +00002266 return __h;
Howard Hinnant3e519522010-05-11 19:42:16 +00002267}
2268
Eric Fiselierfcd02212016-02-11 11:59:44 +00002269#else // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00002270
2271template <class _Tp, class _Hash, class _Equal, class _Alloc>
2272typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Eric Fiselierfcd02212016-02-11 11:59:44 +00002273__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(const __container_value_type& __v)
Howard Hinnant3e519522010-05-11 19:42:16 +00002274{
2275 __node_allocator& __na = __node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00002276 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselierfcd02212016-02-11 11:59:44 +00002277 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), __v);
Howard Hinnant3e519522010-05-11 19:42:16 +00002278 __h.get_deleter().__value_constructed = true;
2279 __h->__hash_ = hash_function()(__h->__value_);
2280 __h->__next_ = nullptr;
Dimitry Andric251c6292015-08-19 06:43:33 +00002281 return _LIBCPP_EXPLICIT_MOVE(__h); // explicitly moved for C++03
Howard Hinnant3e519522010-05-11 19:42:16 +00002282}
2283
Howard Hinnant3e519522010-05-11 19:42:16 +00002284template <class _Tp, class _Hash, class _Equal, class _Alloc>
2285typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Eric Fiselierfcd02212016-02-11 11:59:44 +00002286__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node_hash(size_t __hash,
2287 const __container_value_type& __v)
Howard Hinnant3e519522010-05-11 19:42:16 +00002288{
2289 __node_allocator& __na = __node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00002290 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselierfcd02212016-02-11 11:59:44 +00002291 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), __v);
Howard Hinnant3e519522010-05-11 19:42:16 +00002292 __h.get_deleter().__value_constructed = true;
2293 __h->__hash_ = __hash;
2294 __h->__next_ = nullptr;
Dimitry Andric251c6292015-08-19 06:43:33 +00002295 return _LIBCPP_EXPLICIT_MOVE(__h); // explicitly moved for C++03
Howard Hinnant3e519522010-05-11 19:42:16 +00002296}
2297
Eric Fiselierfcd02212016-02-11 11:59:44 +00002298#endif // _LIBCPP_CXX03_LANG
2299
Howard Hinnant3e519522010-05-11 19:42:16 +00002300template <class _Tp, class _Hash, class _Equal, class _Alloc>
2301typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2302__hash_table<_Tp, _Hash, _Equal, _Alloc>::erase(const_iterator __p)
2303{
Eric Fiselier40492ba2016-07-23 20:36:55 +00002304 __next_pointer __np = __p.__node_;
Howard Hinnantb24c8022013-07-23 22:01:58 +00002305#if _LIBCPP_DEBUG_LEVEL >= 2
2306 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2307 "unordered container erase(iterator) called with an iterator not"
2308 " referring to this container");
2309 _LIBCPP_ASSERT(__p != end(),
2310 "unordered container erase(iterator) called with a non-dereferenceable iterator");
2311 iterator __r(__np, this);
2312#else
Howard Hinnant3e519522010-05-11 19:42:16 +00002313 iterator __r(__np);
Howard Hinnantb24c8022013-07-23 22:01:58 +00002314#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002315 ++__r;
2316 remove(__p);
2317 return __r;
2318}
2319
2320template <class _Tp, class _Hash, class _Equal, class _Alloc>
2321typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2322__hash_table<_Tp, _Hash, _Equal, _Alloc>::erase(const_iterator __first,
2323 const_iterator __last)
2324{
Howard Hinnantb24c8022013-07-23 22:01:58 +00002325#if _LIBCPP_DEBUG_LEVEL >= 2
2326 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
2327 "unodered container::erase(iterator, iterator) called with an iterator not"
2328 " referring to this unodered container");
2329 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__last) == this,
2330 "unodered container::erase(iterator, iterator) called with an iterator not"
2331 " referring to this unodered container");
2332#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002333 for (const_iterator __p = __first; __first != __last; __p = __first)
2334 {
2335 ++__first;
2336 erase(__p);
2337 }
Eric Fiselier40492ba2016-07-23 20:36:55 +00002338 __next_pointer __np = __last.__node_;
Howard Hinnantb24c8022013-07-23 22:01:58 +00002339#if _LIBCPP_DEBUG_LEVEL >= 2
2340 return iterator (__np, this);
2341#else
Howard Hinnant3e519522010-05-11 19:42:16 +00002342 return iterator (__np);
Howard Hinnantb24c8022013-07-23 22:01:58 +00002343#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002344}
2345
2346template <class _Tp, class _Hash, class _Equal, class _Alloc>
2347template <class _Key>
2348typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2349__hash_table<_Tp, _Hash, _Equal, _Alloc>::__erase_unique(const _Key& __k)
2350{
2351 iterator __i = find(__k);
2352 if (__i == end())
2353 return 0;
2354 erase(__i);
2355 return 1;
2356}
2357
2358template <class _Tp, class _Hash, class _Equal, class _Alloc>
2359template <class _Key>
2360typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2361__hash_table<_Tp, _Hash, _Equal, _Alloc>::__erase_multi(const _Key& __k)
2362{
2363 size_type __r = 0;
2364 iterator __i = find(__k);
2365 if (__i != end())
2366 {
2367 iterator __e = end();
2368 do
2369 {
2370 erase(__i++);
2371 ++__r;
2372 } while (__i != __e && key_eq()(*__i, __k));
2373 }
2374 return __r;
2375}
2376
2377template <class _Tp, class _Hash, class _Equal, class _Alloc>
2378typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Howard Hinnant37141072011-06-04 18:54:24 +00002379__hash_table<_Tp, _Hash, _Equal, _Alloc>::remove(const_iterator __p) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00002380{
2381 // current node
Eric Fiselier40492ba2016-07-23 20:36:55 +00002382 __next_pointer __cn = __p.__node_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002383 size_type __bc = bucket_count();
Eric Fiselier40492ba2016-07-23 20:36:55 +00002384 size_t __chash = __constrain_hash(__cn->__hash(), __bc);
Howard Hinnant3e519522010-05-11 19:42:16 +00002385 // find previous node
Eric Fiselier40492ba2016-07-23 20:36:55 +00002386 __next_pointer __pn = __bucket_list_[__chash];
Howard Hinnant3e519522010-05-11 19:42:16 +00002387 for (; __pn->__next_ != __cn; __pn = __pn->__next_)
2388 ;
2389 // Fix up __bucket_list_
2390 // if __pn is not in same bucket (before begin is not in same bucket) &&
2391 // if __cn->__next_ is not in same bucket (nullptr is not in same bucket)
Eric Fiselier40492ba2016-07-23 20:36:55 +00002392 if (__pn == __p1_.first().__ptr()
2393 || __constrain_hash(__pn->__hash(), __bc) != __chash)
Howard Hinnant3e519522010-05-11 19:42:16 +00002394 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00002395 if (__cn->__next_ == nullptr
2396 || __constrain_hash(__cn->__next_->__hash(), __bc) != __chash)
Howard Hinnant3e519522010-05-11 19:42:16 +00002397 __bucket_list_[__chash] = nullptr;
2398 }
2399 // if __cn->__next_ is not in same bucket (nullptr is in same bucket)
2400 if (__cn->__next_ != nullptr)
2401 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00002402 size_t __nhash = __constrain_hash(__cn->__next_->__hash(), __bc);
Howard Hinnant3e519522010-05-11 19:42:16 +00002403 if (__nhash != __chash)
2404 __bucket_list_[__nhash] = __pn;
2405 }
2406 // remove __cn
2407 __pn->__next_ = __cn->__next_;
2408 __cn->__next_ = nullptr;
2409 --size();
Howard Hinnantb24c8022013-07-23 22:01:58 +00002410#if _LIBCPP_DEBUG_LEVEL >= 2
2411 __c_node* __c = __get_db()->__find_c_and_lock(this);
2412 for (__i_node** __p = __c->end_; __p != __c->beg_; )
2413 {
2414 --__p;
2415 iterator* __i = static_cast<iterator*>((*__p)->__i_);
2416 if (__i->__node_ == __cn)
2417 {
2418 (*__p)->__c_ = nullptr;
2419 if (--__c->end_ != __p)
2420 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
2421 }
2422 }
2423 __get_db()->unlock();
2424#endif
Eric Fiselier40492ba2016-07-23 20:36:55 +00002425 return __node_holder(__cn->__upcast(), _Dp(__node_alloc(), true));
Howard Hinnant3e519522010-05-11 19:42:16 +00002426}
2427
2428template <class _Tp, class _Hash, class _Equal, class _Alloc>
2429template <class _Key>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00002430inline
Howard Hinnant3e519522010-05-11 19:42:16 +00002431typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2432__hash_table<_Tp, _Hash, _Equal, _Alloc>::__count_unique(const _Key& __k) const
2433{
2434 return static_cast<size_type>(find(__k) != end());
2435}
2436
2437template <class _Tp, class _Hash, class _Equal, class _Alloc>
2438template <class _Key>
2439typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2440__hash_table<_Tp, _Hash, _Equal, _Alloc>::__count_multi(const _Key& __k) const
2441{
2442 size_type __r = 0;
2443 const_iterator __i = find(__k);
2444 if (__i != end())
2445 {
2446 const_iterator __e = end();
2447 do
2448 {
2449 ++__i;
2450 ++__r;
2451 } while (__i != __e && key_eq()(*__i, __k));
2452 }
2453 return __r;
2454}
2455
2456template <class _Tp, class _Hash, class _Equal, class _Alloc>
2457template <class _Key>
2458pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator,
2459 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator>
2460__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_unique(
2461 const _Key& __k)
2462{
2463 iterator __i = find(__k);
2464 iterator __j = __i;
2465 if (__i != end())
2466 ++__j;
2467 return pair<iterator, iterator>(__i, __j);
2468}
2469
2470template <class _Tp, class _Hash, class _Equal, class _Alloc>
2471template <class _Key>
2472pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator,
2473 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator>
2474__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_unique(
2475 const _Key& __k) const
2476{
2477 const_iterator __i = find(__k);
2478 const_iterator __j = __i;
2479 if (__i != end())
2480 ++__j;
2481 return pair<const_iterator, const_iterator>(__i, __j);
2482}
2483
2484template <class _Tp, class _Hash, class _Equal, class _Alloc>
2485template <class _Key>
2486pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator,
2487 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator>
2488__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_multi(
2489 const _Key& __k)
2490{
2491 iterator __i = find(__k);
2492 iterator __j = __i;
2493 if (__i != end())
2494 {
2495 iterator __e = end();
2496 do
2497 {
2498 ++__j;
2499 } while (__j != __e && key_eq()(*__j, __k));
2500 }
2501 return pair<iterator, iterator>(__i, __j);
2502}
2503
2504template <class _Tp, class _Hash, class _Equal, class _Alloc>
2505template <class _Key>
2506pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator,
2507 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator>
2508__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_multi(
2509 const _Key& __k) const
2510{
2511 const_iterator __i = find(__k);
2512 const_iterator __j = __i;
2513 if (__i != end())
2514 {
2515 const_iterator __e = end();
2516 do
2517 {
2518 ++__j;
2519 } while (__j != __e && key_eq()(*__j, __k));
2520 }
2521 return pair<const_iterator, const_iterator>(__i, __j);
2522}
2523
2524template <class _Tp, class _Hash, class _Equal, class _Alloc>
2525void
2526__hash_table<_Tp, _Hash, _Equal, _Alloc>::swap(__hash_table& __u)
Eric Fiselier87a82492015-07-18 20:40:46 +00002527#if _LIBCPP_STD_VER <= 11
Howard Hinnant37141072011-06-04 18:54:24 +00002528 _NOEXCEPT_(
Marshall Clowe3fbe142015-07-13 20:04:56 +00002529 __is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value
Marshall Clowe3fbe142015-07-13 20:04:56 +00002530 && (!allocator_traits<__pointer_allocator>::propagate_on_container_swap::value
2531 || __is_nothrow_swappable<__pointer_allocator>::value)
2532 && (!__node_traits::propagate_on_container_swap::value
2533 || __is_nothrow_swappable<__node_allocator>::value)
Marshall Clowe3fbe142015-07-13 20:04:56 +00002534 )
Eric Fiselier87a82492015-07-18 20:40:46 +00002535#else
2536 _NOEXCEPT_(__is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value)
2537#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002538{
2539 {
2540 __node_pointer_pointer __npp = __bucket_list_.release();
2541 __bucket_list_.reset(__u.__bucket_list_.release());
2542 __u.__bucket_list_.reset(__npp);
2543 }
Howard Hinnantce48a112011-06-30 21:18:19 +00002544 _VSTD::swap(__bucket_list_.get_deleter().size(), __u.__bucket_list_.get_deleter().size());
Marshall Clowe3fbe142015-07-13 20:04:56 +00002545 __swap_allocator(__bucket_list_.get_deleter().__alloc(),
Howard Hinnant3e519522010-05-11 19:42:16 +00002546 __u.__bucket_list_.get_deleter().__alloc());
Marshall Clowe3fbe142015-07-13 20:04:56 +00002547 __swap_allocator(__node_alloc(), __u.__node_alloc());
Howard Hinnantce48a112011-06-30 21:18:19 +00002548 _VSTD::swap(__p1_.first().__next_, __u.__p1_.first().__next_);
Howard Hinnant3e519522010-05-11 19:42:16 +00002549 __p2_.swap(__u.__p2_);
2550 __p3_.swap(__u.__p3_);
2551 if (size() > 0)
Eric Fiselier40492ba2016-07-23 20:36:55 +00002552 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash(), bucket_count())] =
2553 __p1_.first().__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00002554 if (__u.size() > 0)
Eric Fiselier40492ba2016-07-23 20:36:55 +00002555 __u.__bucket_list_[__constrain_hash(__u.__p1_.first().__next_->__hash(), __u.bucket_count())] =
2556 __u.__p1_.first().__ptr();
Howard Hinnantb24c8022013-07-23 22:01:58 +00002557#if _LIBCPP_DEBUG_LEVEL >= 2
2558 __get_db()->swap(this, &__u);
2559#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002560}
2561
2562template <class _Tp, class _Hash, class _Equal, class _Alloc>
2563typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2564__hash_table<_Tp, _Hash, _Equal, _Alloc>::bucket_size(size_type __n) const
2565{
Howard Hinnante5c13de2013-07-29 19:05:47 +00002566 _LIBCPP_ASSERT(__n < bucket_count(),
2567 "unordered container::bucket_size(n) called with n >= bucket_count()");
Eric Fiselier40492ba2016-07-23 20:36:55 +00002568 __next_pointer __np = __bucket_list_[__n];
Howard Hinnant3e519522010-05-11 19:42:16 +00002569 size_type __bc = bucket_count();
2570 size_type __r = 0;
2571 if (__np != nullptr)
2572 {
2573 for (__np = __np->__next_; __np != nullptr &&
Eric Fiselier40492ba2016-07-23 20:36:55 +00002574 __constrain_hash(__np->__hash(), __bc) == __n;
Howard Hinnant3e519522010-05-11 19:42:16 +00002575 __np = __np->__next_, ++__r)
2576 ;
2577 }
2578 return __r;
2579}
2580
Howard Hinnant37141072011-06-04 18:54:24 +00002581template <class _Tp, class _Hash, class _Equal, class _Alloc>
2582inline _LIBCPP_INLINE_VISIBILITY
2583void
2584swap(__hash_table<_Tp, _Hash, _Equal, _Alloc>& __x,
2585 __hash_table<_Tp, _Hash, _Equal, _Alloc>& __y)
2586 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
2587{
2588 __x.swap(__y);
2589}
2590
Howard Hinnantb24c8022013-07-23 22:01:58 +00002591#if _LIBCPP_DEBUG_LEVEL >= 2
2592
2593template <class _Tp, class _Hash, class _Equal, class _Alloc>
2594bool
2595__hash_table<_Tp, _Hash, _Equal, _Alloc>::__dereferenceable(const const_iterator* __i) const
2596{
2597 return __i->__node_ != nullptr;
2598}
2599
2600template <class _Tp, class _Hash, class _Equal, class _Alloc>
2601bool
2602__hash_table<_Tp, _Hash, _Equal, _Alloc>::__decrementable(const const_iterator*) const
2603{
2604 return false;
2605}
2606
2607template <class _Tp, class _Hash, class _Equal, class _Alloc>
2608bool
2609__hash_table<_Tp, _Hash, _Equal, _Alloc>::__addable(const const_iterator*, ptrdiff_t) const
2610{
2611 return false;
2612}
2613
2614template <class _Tp, class _Hash, class _Equal, class _Alloc>
2615bool
2616__hash_table<_Tp, _Hash, _Equal, _Alloc>::__subscriptable(const const_iterator*, ptrdiff_t) const
2617{
2618 return false;
2619}
2620
2621#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant3e519522010-05-11 19:42:16 +00002622_LIBCPP_END_NAMESPACE_STD
2623
2624#endif // _LIBCPP__HASH_TABLE