blob: 9a0d441b37b8990addff9b4d0b85c9107f9be056 [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>(
Eric Fiselier341c9dd2016-11-23 09:16:12 +00001008 __node_traits::max_size(__node_alloc()),
Eric Fiselier55b31b4e2016-11-23 01:18:56 +00001009 numeric_limits<difference_type >::max()
1010 );
Howard Hinnant3e519522010-05-11 19:42:16 +00001011 }
1012
1013 pair<iterator, bool> __node_insert_unique(__node_pointer __nd);
1014 iterator __node_insert_multi(__node_pointer __nd);
1015 iterator __node_insert_multi(const_iterator __p,
1016 __node_pointer __nd);
1017
Eric Fiselierfcd02212016-02-11 11:59:44 +00001018#ifndef _LIBCPP_CXX03_LANG
1019 template <class _Key, class ..._Args>
Duncan P. N. Exon Smithfde79b42016-03-17 20:45:20 +00001020 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierfcd02212016-02-11 11:59:44 +00001021 pair<iterator, bool> __emplace_unique_key_args(_Key const& __k, _Args&&... __args);
Howard Hinnant3e519522010-05-11 19:42:16 +00001022
Eric Fiselierfcd02212016-02-11 11:59:44 +00001023 template <class... _Args>
Duncan P. N. Exon Smithfde79b42016-03-17 20:45:20 +00001024 _LIBCPP_INLINE_VISIBILITY
1025 pair<iterator, bool> __emplace_unique_impl(_Args&&... __args);
1026
1027 template <class _Pp>
1028 _LIBCPP_INLINE_VISIBILITY
1029 pair<iterator, bool> __emplace_unique(_Pp&& __x) {
1030 return __emplace_unique_extract_key(_VSTD::forward<_Pp>(__x),
1031 __can_extract_key<_Pp, key_type>());
1032 }
Eric Fiselier50088682016-04-16 00:23:12 +00001033
1034 template <class _First, class _Second>
1035 _LIBCPP_INLINE_VISIBILITY
1036 typename enable_if<
1037 __can_extract_map_key<_First, key_type, __container_value_type>::value,
1038 pair<iterator, bool>
1039 >::type __emplace_unique(_First&& __f, _Second&& __s) {
1040 return __emplace_unique_key_args(__f, _VSTD::forward<_First>(__f),
1041 _VSTD::forward<_Second>(__s));
1042 }
1043
Eric Fiselierfcd02212016-02-11 11:59:44 +00001044 template <class... _Args>
Duncan P. N. Exon Smithfde79b42016-03-17 20:45:20 +00001045 _LIBCPP_INLINE_VISIBILITY
1046 pair<iterator, bool> __emplace_unique(_Args&&... __args) {
1047 return __emplace_unique_impl(_VSTD::forward<_Args>(__args)...);
1048 }
1049
1050 template <class _Pp>
1051 _LIBCPP_INLINE_VISIBILITY
1052 pair<iterator, bool>
1053 __emplace_unique_extract_key(_Pp&& __x, __extract_key_fail_tag) {
1054 return __emplace_unique_impl(_VSTD::forward<_Pp>(__x));
1055 }
1056 template <class _Pp>
1057 _LIBCPP_INLINE_VISIBILITY
1058 pair<iterator, bool>
1059 __emplace_unique_extract_key(_Pp&& __x, __extract_key_self_tag) {
1060 return __emplace_unique_key_args(__x, _VSTD::forward<_Pp>(__x));
1061 }
1062 template <class _Pp>
1063 _LIBCPP_INLINE_VISIBILITY
1064 pair<iterator, bool>
1065 __emplace_unique_extract_key(_Pp&& __x, __extract_key_first_tag) {
1066 return __emplace_unique_key_args(__x.first, _VSTD::forward<_Pp>(__x));
1067 }
1068
1069 template <class... _Args>
1070 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierfcd02212016-02-11 11:59:44 +00001071 iterator __emplace_multi(_Args&&... __args);
1072 template <class... _Args>
Duncan P. N. Exon Smithfde79b42016-03-17 20:45:20 +00001073 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierfcd02212016-02-11 11:59:44 +00001074 iterator __emplace_hint_multi(const_iterator __p, _Args&&... __args);
1075
1076
Eric Fiselierde3f2b32015-06-13 07:18:32 +00001077 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierfcd02212016-02-11 11:59:44 +00001078 pair<iterator, bool>
1079 __insert_unique(__container_value_type&& __x) {
1080 return __emplace_unique_key_args(_NodeTypes::__get_key(__x), _VSTD::move(__x));
1081 }
1082
1083 template <class _Pp, class = typename enable_if<
1084 !__is_same_uncvref<_Pp, __container_value_type>::value
1085 >::type>
Eric Fiselierde3f2b32015-06-13 07:18:32 +00001086 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierfcd02212016-02-11 11:59:44 +00001087 pair<iterator, bool> __insert_unique(_Pp&& __x) {
1088 return __emplace_unique(_VSTD::forward<_Pp>(__x));
1089 }
1090
1091 template <class _Pp>
1092 _LIBCPP_INLINE_VISIBILITY
1093 iterator __insert_multi(_Pp&& __x) {
1094 return __emplace_multi(_VSTD::forward<_Pp>(__x));
1095 }
1096
1097 template <class _Pp>
1098 _LIBCPP_INLINE_VISIBILITY
1099 iterator __insert_multi(const_iterator __p, _Pp&& __x) {
1100 return __emplace_hint_multi(__p, _VSTD::forward<_Pp>(__x));
1101 }
1102
1103#else // !defined(_LIBCPP_CXX03_LANG)
1104 template <class _Key, class _Args>
Eric Fiselier4271d012016-09-25 04:05:46 +00001105 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierfcd02212016-02-11 11:59:44 +00001106 pair<iterator, bool> __emplace_unique_key_args(_Key const&, _Args& __args);
1107
1108 iterator __insert_multi(const __container_value_type& __x);
1109 iterator __insert_multi(const_iterator __p, const __container_value_type& __x);
Eric Fiselierde3f2b32015-06-13 07:18:32 +00001110#endif
1111
Eric Fiselierfcd02212016-02-11 11:59:44 +00001112 _LIBCPP_INLINE_VISIBILITY
1113 pair<iterator, bool> __insert_unique(const __container_value_type& __x) {
1114 return __emplace_unique_key_args(_NodeTypes::__get_key(__x), __x);
1115 }
Howard Hinnant3e519522010-05-11 19:42:16 +00001116
Howard Hinnant37141072011-06-04 18:54:24 +00001117 void clear() _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +00001118 void rehash(size_type __n);
Howard Hinnant43d99232010-09-21 17:32:39 +00001119 _LIBCPP_INLINE_VISIBILITY void reserve(size_type __n)
Howard Hinnant3e519522010-05-11 19:42:16 +00001120 {rehash(static_cast<size_type>(ceil(__n / max_load_factor())));}
Howard Hinnant43d99232010-09-21 17:32:39 +00001121
1122 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001123 size_type bucket_count() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001124 {
1125 return __bucket_list_.get_deleter().size();
1126 }
1127
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001128 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001129 iterator begin() _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001130 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001131 iterator end() _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001132 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001133 const_iterator begin() const _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001134 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001135 const_iterator end() const _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +00001136
1137 template <class _Key>
Howard Hinnant43d99232010-09-21 17:32:39 +00001138 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001139 size_type bucket(const _Key& __k) const
Howard Hinnante5c13de2013-07-29 19:05:47 +00001140 {
1141 _LIBCPP_ASSERT(bucket_count() > 0,
1142 "unordered container::bucket(key) called when bucket_count() == 0");
1143 return __constrain_hash(hash_function()(__k), bucket_count());
1144 }
Howard Hinnant3e519522010-05-11 19:42:16 +00001145
1146 template <class _Key>
1147 iterator find(const _Key& __x);
1148 template <class _Key>
1149 const_iterator find(const _Key& __x) const;
1150
Howard Hinnantc003db12011-11-29 18:15:50 +00001151 typedef __hash_node_destructor<__node_allocator> _Dp;
1152 typedef unique_ptr<__node, _Dp> __node_holder;
Howard Hinnant3e519522010-05-11 19:42:16 +00001153
1154 iterator erase(const_iterator __p);
1155 iterator erase(const_iterator __first, const_iterator __last);
1156 template <class _Key>
1157 size_type __erase_unique(const _Key& __k);
1158 template <class _Key>
1159 size_type __erase_multi(const _Key& __k);
Howard Hinnant37141072011-06-04 18:54:24 +00001160 __node_holder remove(const_iterator __p) _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +00001161
1162 template <class _Key>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001163 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001164 size_type __count_unique(const _Key& __k) const;
1165 template <class _Key>
1166 size_type __count_multi(const _Key& __k) const;
1167
1168 template <class _Key>
1169 pair<iterator, iterator>
1170 __equal_range_unique(const _Key& __k);
1171 template <class _Key>
1172 pair<const_iterator, const_iterator>
1173 __equal_range_unique(const _Key& __k) const;
1174
1175 template <class _Key>
1176 pair<iterator, iterator>
1177 __equal_range_multi(const _Key& __k);
1178 template <class _Key>
1179 pair<const_iterator, const_iterator>
1180 __equal_range_multi(const _Key& __k) const;
1181
Howard Hinnant37141072011-06-04 18:54:24 +00001182 void swap(__hash_table& __u)
Eric Fiselier87a82492015-07-18 20:40:46 +00001183#if _LIBCPP_STD_VER <= 11
Eric Fiselier780b51d2016-12-28 05:53:01 +00001184 _NOEXCEPT_DEBUG_(
Marshall Clowe3fbe142015-07-13 20:04:56 +00001185 __is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value
Marshall Clowe3fbe142015-07-13 20:04:56 +00001186 && (!allocator_traits<__pointer_allocator>::propagate_on_container_swap::value
1187 || __is_nothrow_swappable<__pointer_allocator>::value)
1188 && (!__node_traits::propagate_on_container_swap::value
1189 || __is_nothrow_swappable<__node_allocator>::value)
Marshall Clowe3fbe142015-07-13 20:04:56 +00001190 );
Eric Fiselier87a82492015-07-18 20:40:46 +00001191#else
Eric Fiselier780b51d2016-12-28 05:53:01 +00001192 _NOEXCEPT_DEBUG_(__is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value);
Eric Fiselier87a82492015-07-18 20:40:46 +00001193#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001194
Howard Hinnant43d99232010-09-21 17:32:39 +00001195 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001196 size_type max_bucket_count() const _NOEXCEPT
Eric Fiselier55b31b4e2016-11-23 01:18:56 +00001197 {return max_size(); }
Howard Hinnant3e519522010-05-11 19:42:16 +00001198 size_type bucket_size(size_type __n) const;
Howard Hinnant37141072011-06-04 18:54:24 +00001199 _LIBCPP_INLINE_VISIBILITY float load_factor() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001200 {
1201 size_type __bc = bucket_count();
1202 return __bc != 0 ? (float)size() / __bc : 0.f;
1203 }
Howard Hinnant37141072011-06-04 18:54:24 +00001204 _LIBCPP_INLINE_VISIBILITY void max_load_factor(float __mlf) _NOEXCEPT
Howard Hinnante5c13de2013-07-29 19:05:47 +00001205 {
1206 _LIBCPP_ASSERT(__mlf > 0,
1207 "unordered container::max_load_factor(lf) called with lf <= 0");
1208 max_load_factor() = _VSTD::max(__mlf, load_factor());
1209 }
Howard Hinnant3e519522010-05-11 19:42:16 +00001210
Howard Hinnantb24c8022013-07-23 22:01:58 +00001211 _LIBCPP_INLINE_VISIBILITY
1212 local_iterator
1213 begin(size_type __n)
1214 {
Howard Hinnante5c13de2013-07-29 19:05:47 +00001215 _LIBCPP_ASSERT(__n < bucket_count(),
1216 "unordered container::begin(n) called with n >= bucket_count()");
Howard Hinnantb24c8022013-07-23 22:01:58 +00001217#if _LIBCPP_DEBUG_LEVEL >= 2
1218 return local_iterator(__bucket_list_[__n], __n, bucket_count(), this);
1219#else
1220 return local_iterator(__bucket_list_[__n], __n, bucket_count());
1221#endif
1222 }
1223
1224 _LIBCPP_INLINE_VISIBILITY
1225 local_iterator
1226 end(size_type __n)
1227 {
Howard Hinnante5c13de2013-07-29 19:05:47 +00001228 _LIBCPP_ASSERT(__n < bucket_count(),
1229 "unordered container::end(n) called with n >= bucket_count()");
Howard Hinnantb24c8022013-07-23 22:01:58 +00001230#if _LIBCPP_DEBUG_LEVEL >= 2
1231 return local_iterator(nullptr, __n, bucket_count(), this);
1232#else
1233 return local_iterator(nullptr, __n, bucket_count());
1234#endif
1235 }
1236
1237 _LIBCPP_INLINE_VISIBILITY
1238 const_local_iterator
1239 cbegin(size_type __n) const
1240 {
Howard Hinnante5c13de2013-07-29 19:05:47 +00001241 _LIBCPP_ASSERT(__n < bucket_count(),
1242 "unordered container::cbegin(n) called with n >= bucket_count()");
Howard Hinnantb24c8022013-07-23 22:01:58 +00001243#if _LIBCPP_DEBUG_LEVEL >= 2
1244 return const_local_iterator(__bucket_list_[__n], __n, bucket_count(), this);
1245#else
1246 return const_local_iterator(__bucket_list_[__n], __n, bucket_count());
1247#endif
1248 }
1249
1250 _LIBCPP_INLINE_VISIBILITY
1251 const_local_iterator
1252 cend(size_type __n) const
1253 {
Howard Hinnante5c13de2013-07-29 19:05:47 +00001254 _LIBCPP_ASSERT(__n < bucket_count(),
1255 "unordered container::cend(n) called with n >= bucket_count()");
Howard Hinnantb24c8022013-07-23 22:01:58 +00001256#if _LIBCPP_DEBUG_LEVEL >= 2
1257 return const_local_iterator(nullptr, __n, bucket_count(), this);
1258#else
1259 return const_local_iterator(nullptr, __n, bucket_count());
1260#endif
1261 }
1262
1263#if _LIBCPP_DEBUG_LEVEL >= 2
1264
1265 bool __dereferenceable(const const_iterator* __i) const;
1266 bool __decrementable(const const_iterator* __i) const;
1267 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1268 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1269
1270#endif // _LIBCPP_DEBUG_LEVEL >= 2
1271
Howard Hinnant3e519522010-05-11 19:42:16 +00001272private:
1273 void __rehash(size_type __n);
1274
Eric Fiselierfcd02212016-02-11 11:59:44 +00001275#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001276 template <class ..._Args>
Eric Fiselierfcd02212016-02-11 11:59:44 +00001277 __node_holder __construct_node(_Args&& ...__args);
1278
1279 template <class _First, class ..._Rest>
1280 __node_holder __construct_node_hash(size_t __hash, _First&& __f, _Rest&&... __rest);
1281#else // _LIBCPP_CXX03_LANG
1282 __node_holder __construct_node(const __container_value_type& __v);
1283 __node_holder __construct_node_hash(size_t __hash, const __container_value_type& __v);
Howard Hinnant3e519522010-05-11 19:42:16 +00001284#endif
Eric Fiselierfcd02212016-02-11 11:59:44 +00001285
Howard Hinnant3e519522010-05-11 19:42:16 +00001286
Howard Hinnant43d99232010-09-21 17:32:39 +00001287 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001288 void __copy_assign_alloc(const __hash_table& __u)
1289 {__copy_assign_alloc(__u, integral_constant<bool,
1290 __node_traits::propagate_on_container_copy_assignment::value>());}
1291 void __copy_assign_alloc(const __hash_table& __u, true_type);
Howard Hinnant43d99232010-09-21 17:32:39 +00001292 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2063662011-12-01 20:21:04 +00001293 void __copy_assign_alloc(const __hash_table&, false_type) {}
Howard Hinnant3e519522010-05-11 19:42:16 +00001294
Eric Fiselierfcd02212016-02-11 11:59:44 +00001295#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001296 void __move_assign(__hash_table& __u, false_type);
Howard Hinnant37141072011-06-04 18:54:24 +00001297 void __move_assign(__hash_table& __u, true_type)
1298 _NOEXCEPT_(
1299 is_nothrow_move_assignable<__node_allocator>::value &&
1300 is_nothrow_move_assignable<hasher>::value &&
1301 is_nothrow_move_assignable<key_equal>::value);
1302 _LIBCPP_INLINE_VISIBILITY
1303 void __move_assign_alloc(__hash_table& __u)
1304 _NOEXCEPT_(
1305 !__node_traits::propagate_on_container_move_assignment::value ||
1306 (is_nothrow_move_assignable<__pointer_allocator>::value &&
1307 is_nothrow_move_assignable<__node_allocator>::value))
Howard Hinnant3e519522010-05-11 19:42:16 +00001308 {__move_assign_alloc(__u, integral_constant<bool,
1309 __node_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnant43d99232010-09-21 17:32:39 +00001310 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001311 void __move_assign_alloc(__hash_table& __u, true_type)
Howard Hinnant37141072011-06-04 18:54:24 +00001312 _NOEXCEPT_(
1313 is_nothrow_move_assignable<__pointer_allocator>::value &&
1314 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +00001315 {
1316 __bucket_list_.get_deleter().__alloc() =
Howard Hinnantce48a112011-06-30 21:18:19 +00001317 _VSTD::move(__u.__bucket_list_.get_deleter().__alloc());
1318 __node_alloc() = _VSTD::move(__u.__node_alloc());
Howard Hinnant3e519522010-05-11 19:42:16 +00001319 }
Howard Hinnant43d99232010-09-21 17:32:39 +00001320 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001321 void __move_assign_alloc(__hash_table&, false_type) _NOEXCEPT {}
Eric Fiselierfcd02212016-02-11 11:59:44 +00001322#endif // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001323
Eric Fiselier40492ba2016-07-23 20:36:55 +00001324 void __deallocate(__next_pointer __np) _NOEXCEPT;
1325 __next_pointer __detach() _NOEXCEPT;
Howard Hinnant307f8142013-06-22 15:21:29 +00001326
Howard Hinnantf0544c22013-08-12 18:38:34 +00001327 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_map;
1328 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_multimap;
Howard Hinnant3e519522010-05-11 19:42:16 +00001329};
1330
1331template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001332inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001333__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table()
Howard Hinnant37141072011-06-04 18:54:24 +00001334 _NOEXCEPT_(
1335 is_nothrow_default_constructible<__bucket_list>::value &&
1336 is_nothrow_default_constructible<__first_node>::value &&
Eric Fiseliere2e332a2015-12-16 00:53:04 +00001337 is_nothrow_default_constructible<__node_allocator>::value &&
Howard Hinnant37141072011-06-04 18:54:24 +00001338 is_nothrow_default_constructible<hasher>::value &&
1339 is_nothrow_default_constructible<key_equal>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +00001340 : __p2_(0),
1341 __p3_(1.0f)
1342{
1343}
1344
1345template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001346inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001347__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const hasher& __hf,
1348 const key_equal& __eql)
1349 : __bucket_list_(nullptr, __bucket_list_deleter()),
1350 __p1_(),
1351 __p2_(0, __hf),
1352 __p3_(1.0f, __eql)
1353{
1354}
1355
1356template <class _Tp, class _Hash, class _Equal, class _Alloc>
1357__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const hasher& __hf,
1358 const key_equal& __eql,
1359 const allocator_type& __a)
1360 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1361 __p1_(__node_allocator(__a)),
1362 __p2_(0, __hf),
1363 __p3_(1.0f, __eql)
1364{
1365}
1366
1367template <class _Tp, class _Hash, class _Equal, class _Alloc>
1368__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const allocator_type& __a)
1369 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1370 __p1_(__node_allocator(__a)),
1371 __p2_(0),
1372 __p3_(1.0f)
1373{
1374}
1375
1376template <class _Tp, class _Hash, class _Equal, class _Alloc>
1377__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const __hash_table& __u)
1378 : __bucket_list_(nullptr,
1379 __bucket_list_deleter(allocator_traits<__pointer_allocator>::
1380 select_on_container_copy_construction(
1381 __u.__bucket_list_.get_deleter().__alloc()), 0)),
1382 __p1_(allocator_traits<__node_allocator>::
1383 select_on_container_copy_construction(__u.__node_alloc())),
1384 __p2_(0, __u.hash_function()),
1385 __p3_(__u.__p3_)
1386{
1387}
1388
1389template <class _Tp, class _Hash, class _Equal, class _Alloc>
1390__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const __hash_table& __u,
1391 const allocator_type& __a)
1392 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1393 __p1_(__node_allocator(__a)),
1394 __p2_(0, __u.hash_function()),
1395 __p3_(__u.__p3_)
1396{
1397}
1398
Eric Fiselierfcd02212016-02-11 11:59:44 +00001399#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001400
1401template <class _Tp, class _Hash, class _Equal, class _Alloc>
1402__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(__hash_table&& __u)
Howard Hinnant37141072011-06-04 18:54:24 +00001403 _NOEXCEPT_(
1404 is_nothrow_move_constructible<__bucket_list>::value &&
1405 is_nothrow_move_constructible<__first_node>::value &&
Eric Fiseliere2e332a2015-12-16 00:53:04 +00001406 is_nothrow_move_constructible<__node_allocator>::value &&
Howard Hinnant37141072011-06-04 18:54:24 +00001407 is_nothrow_move_constructible<hasher>::value &&
1408 is_nothrow_move_constructible<key_equal>::value)
Howard Hinnantce48a112011-06-30 21:18:19 +00001409 : __bucket_list_(_VSTD::move(__u.__bucket_list_)),
1410 __p1_(_VSTD::move(__u.__p1_)),
1411 __p2_(_VSTD::move(__u.__p2_)),
1412 __p3_(_VSTD::move(__u.__p3_))
Howard Hinnant3e519522010-05-11 19:42:16 +00001413{
1414 if (size() > 0)
1415 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001416 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash(), bucket_count())] =
1417 __p1_.first().__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001418 __u.__p1_.first().__next_ = nullptr;
1419 __u.size() = 0;
1420 }
1421}
1422
1423template <class _Tp, class _Hash, class _Equal, class _Alloc>
1424__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(__hash_table&& __u,
1425 const allocator_type& __a)
1426 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1427 __p1_(__node_allocator(__a)),
Howard Hinnantce48a112011-06-30 21:18:19 +00001428 __p2_(0, _VSTD::move(__u.hash_function())),
1429 __p3_(_VSTD::move(__u.__p3_))
Howard Hinnant3e519522010-05-11 19:42:16 +00001430{
1431 if (__a == allocator_type(__u.__node_alloc()))
1432 {
1433 __bucket_list_.reset(__u.__bucket_list_.release());
1434 __bucket_list_.get_deleter().size() = __u.__bucket_list_.get_deleter().size();
1435 __u.__bucket_list_.get_deleter().size() = 0;
1436 if (__u.size() > 0)
1437 {
1438 __p1_.first().__next_ = __u.__p1_.first().__next_;
1439 __u.__p1_.first().__next_ = nullptr;
Eric Fiselier40492ba2016-07-23 20:36:55 +00001440 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash(), bucket_count())] =
1441 __p1_.first().__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001442 size() = __u.size();
1443 __u.size() = 0;
1444 }
1445 }
1446}
1447
Eric Fiselierfcd02212016-02-11 11:59:44 +00001448#endif // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001449
1450template <class _Tp, class _Hash, class _Equal, class _Alloc>
1451__hash_table<_Tp, _Hash, _Equal, _Alloc>::~__hash_table()
1452{
Marshall Clow3b8669e2016-06-30 22:05:45 +00001453 static_assert((is_copy_constructible<key_equal>::value),
1454 "Predicate must be copy-constructible.");
1455 static_assert((is_copy_constructible<hasher>::value),
1456 "Hasher must be copy-constructible.");
Howard Hinnant3e519522010-05-11 19:42:16 +00001457 __deallocate(__p1_.first().__next_);
Howard Hinnantb24c8022013-07-23 22:01:58 +00001458#if _LIBCPP_DEBUG_LEVEL >= 2
1459 __get_db()->__erase_c(this);
1460#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001461}
1462
1463template <class _Tp, class _Hash, class _Equal, class _Alloc>
1464void
1465__hash_table<_Tp, _Hash, _Equal, _Alloc>::__copy_assign_alloc(
1466 const __hash_table& __u, true_type)
1467{
1468 if (__node_alloc() != __u.__node_alloc())
1469 {
1470 clear();
1471 __bucket_list_.reset();
1472 __bucket_list_.get_deleter().size() = 0;
1473 }
1474 __bucket_list_.get_deleter().__alloc() = __u.__bucket_list_.get_deleter().__alloc();
1475 __node_alloc() = __u.__node_alloc();
1476}
1477
1478template <class _Tp, class _Hash, class _Equal, class _Alloc>
1479__hash_table<_Tp, _Hash, _Equal, _Alloc>&
1480__hash_table<_Tp, _Hash, _Equal, _Alloc>::operator=(const __hash_table& __u)
1481{
1482 if (this != &__u)
1483 {
1484 __copy_assign_alloc(__u);
1485 hash_function() = __u.hash_function();
1486 key_eq() = __u.key_eq();
1487 max_load_factor() = __u.max_load_factor();
1488 __assign_multi(__u.begin(), __u.end());
1489 }
1490 return *this;
1491}
1492
1493template <class _Tp, class _Hash, class _Equal, class _Alloc>
1494void
Eric Fiselier40492ba2016-07-23 20:36:55 +00001495__hash_table<_Tp, _Hash, _Equal, _Alloc>::__deallocate(__next_pointer __np)
Howard Hinnant37141072011-06-04 18:54:24 +00001496 _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001497{
1498 __node_allocator& __na = __node_alloc();
1499 while (__np != nullptr)
1500 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001501 __next_pointer __next = __np->__next_;
Howard Hinnantb24c8022013-07-23 22:01:58 +00001502#if _LIBCPP_DEBUG_LEVEL >= 2
1503 __c_node* __c = __get_db()->__find_c_and_lock(this);
1504 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1505 {
1506 --__p;
1507 iterator* __i = static_cast<iterator*>((*__p)->__i_);
1508 if (__i->__node_ == __np)
1509 {
1510 (*__p)->__c_ = nullptr;
1511 if (--__c->end_ != __p)
1512 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1513 }
1514 }
1515 __get_db()->unlock();
1516#endif
Eric Fiselier40492ba2016-07-23 20:36:55 +00001517 __node_pointer __real_np = __np->__upcast();
1518 __node_traits::destroy(__na, _NodeTypes::__get_ptr(__real_np->__value_));
1519 __node_traits::deallocate(__na, __real_np, 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001520 __np = __next;
1521 }
1522}
1523
1524template <class _Tp, class _Hash, class _Equal, class _Alloc>
Eric Fiselier40492ba2016-07-23 20:36:55 +00001525typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__next_pointer
Howard Hinnant37141072011-06-04 18:54:24 +00001526__hash_table<_Tp, _Hash, _Equal, _Alloc>::__detach() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001527{
1528 size_type __bc = bucket_count();
1529 for (size_type __i = 0; __i < __bc; ++__i)
1530 __bucket_list_[__i] = nullptr;
1531 size() = 0;
Eric Fiselier40492ba2016-07-23 20:36:55 +00001532 __next_pointer __cache = __p1_.first().__next_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001533 __p1_.first().__next_ = nullptr;
1534 return __cache;
1535}
1536
Eric Fiselierfcd02212016-02-11 11:59:44 +00001537#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001538
1539template <class _Tp, class _Hash, class _Equal, class _Alloc>
1540void
1541__hash_table<_Tp, _Hash, _Equal, _Alloc>::__move_assign(
1542 __hash_table& __u, true_type)
Howard Hinnant37141072011-06-04 18:54:24 +00001543 _NOEXCEPT_(
1544 is_nothrow_move_assignable<__node_allocator>::value &&
1545 is_nothrow_move_assignable<hasher>::value &&
1546 is_nothrow_move_assignable<key_equal>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +00001547{
1548 clear();
1549 __bucket_list_.reset(__u.__bucket_list_.release());
1550 __bucket_list_.get_deleter().size() = __u.__bucket_list_.get_deleter().size();
1551 __u.__bucket_list_.get_deleter().size() = 0;
1552 __move_assign_alloc(__u);
1553 size() = __u.size();
Howard Hinnantce48a112011-06-30 21:18:19 +00001554 hash_function() = _VSTD::move(__u.hash_function());
Howard Hinnant3e519522010-05-11 19:42:16 +00001555 max_load_factor() = __u.max_load_factor();
Howard Hinnantce48a112011-06-30 21:18:19 +00001556 key_eq() = _VSTD::move(__u.key_eq());
Howard Hinnant3e519522010-05-11 19:42:16 +00001557 __p1_.first().__next_ = __u.__p1_.first().__next_;
1558 if (size() > 0)
1559 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001560 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash(), bucket_count())] =
1561 __p1_.first().__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001562 __u.__p1_.first().__next_ = nullptr;
1563 __u.size() = 0;
1564 }
Howard Hinnantb24c8022013-07-23 22:01:58 +00001565#if _LIBCPP_DEBUG_LEVEL >= 2
1566 __get_db()->swap(this, &__u);
1567#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001568}
1569
1570template <class _Tp, class _Hash, class _Equal, class _Alloc>
1571void
1572__hash_table<_Tp, _Hash, _Equal, _Alloc>::__move_assign(
1573 __hash_table& __u, false_type)
1574{
1575 if (__node_alloc() == __u.__node_alloc())
1576 __move_assign(__u, true_type());
1577 else
1578 {
Howard Hinnantce48a112011-06-30 21:18:19 +00001579 hash_function() = _VSTD::move(__u.hash_function());
1580 key_eq() = _VSTD::move(__u.key_eq());
Howard Hinnant3e519522010-05-11 19:42:16 +00001581 max_load_factor() = __u.max_load_factor();
1582 if (bucket_count() != 0)
1583 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001584 __next_pointer __cache = __detach();
Howard Hinnant3e519522010-05-11 19:42:16 +00001585#ifndef _LIBCPP_NO_EXCEPTIONS
1586 try
1587 {
Howard Hinnantb3371f62010-08-22 00:02:43 +00001588#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001589 const_iterator __i = __u.begin();
1590 while (__cache != nullptr && __u.size() != 0)
1591 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001592 __cache->__upcast()->__value_ =
1593 _VSTD::move(__u.remove(__i++)->__value_);
1594 __next_pointer __next = __cache->__next_;
1595 __node_insert_multi(__cache->__upcast());
Howard Hinnant3e519522010-05-11 19:42:16 +00001596 __cache = __next;
1597 }
1598#ifndef _LIBCPP_NO_EXCEPTIONS
1599 }
1600 catch (...)
1601 {
1602 __deallocate(__cache);
1603 throw;
1604 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00001605#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001606 __deallocate(__cache);
1607 }
1608 const_iterator __i = __u.begin();
1609 while (__u.size() != 0)
1610 {
Eric Fiselierfcd02212016-02-11 11:59:44 +00001611 __node_holder __h = __construct_node(_NodeTypes::__move(__u.remove(__i++)->__value_));
Howard Hinnant3e519522010-05-11 19:42:16 +00001612 __node_insert_multi(__h.get());
1613 __h.release();
1614 }
1615 }
1616}
1617
1618template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001619inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001620__hash_table<_Tp, _Hash, _Equal, _Alloc>&
1621__hash_table<_Tp, _Hash, _Equal, _Alloc>::operator=(__hash_table&& __u)
Howard Hinnant37141072011-06-04 18:54:24 +00001622 _NOEXCEPT_(
1623 __node_traits::propagate_on_container_move_assignment::value &&
1624 is_nothrow_move_assignable<__node_allocator>::value &&
1625 is_nothrow_move_assignable<hasher>::value &&
1626 is_nothrow_move_assignable<key_equal>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +00001627{
1628 __move_assign(__u, integral_constant<bool,
1629 __node_traits::propagate_on_container_move_assignment::value>());
1630 return *this;
1631}
1632
Eric Fiselierfcd02212016-02-11 11:59:44 +00001633#endif // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001634
1635template <class _Tp, class _Hash, class _Equal, class _Alloc>
1636template <class _InputIterator>
1637void
1638__hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_unique(_InputIterator __first,
1639 _InputIterator __last)
1640{
Eric Fiselierfcd02212016-02-11 11:59:44 +00001641 typedef iterator_traits<_InputIterator> _ITraits;
1642 typedef typename _ITraits::value_type _ItValueType;
1643 static_assert((is_same<_ItValueType, __container_value_type>::value),
1644 "__assign_unique may only be called with the containers value type");
1645
Howard Hinnant3e519522010-05-11 19:42:16 +00001646 if (bucket_count() != 0)
1647 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001648 __next_pointer __cache = __detach();
Howard Hinnant3e519522010-05-11 19:42:16 +00001649#ifndef _LIBCPP_NO_EXCEPTIONS
1650 try
1651 {
Howard Hinnantb3371f62010-08-22 00:02:43 +00001652#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001653 for (; __cache != nullptr && __first != __last; ++__first)
1654 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001655 __cache->__upcast()->__value_ = *__first;
1656 __next_pointer __next = __cache->__next_;
1657 __node_insert_unique(__cache->__upcast());
Howard Hinnant3e519522010-05-11 19:42:16 +00001658 __cache = __next;
1659 }
1660#ifndef _LIBCPP_NO_EXCEPTIONS
1661 }
1662 catch (...)
1663 {
1664 __deallocate(__cache);
1665 throw;
1666 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00001667#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001668 __deallocate(__cache);
1669 }
1670 for (; __first != __last; ++__first)
1671 __insert_unique(*__first);
1672}
1673
1674template <class _Tp, class _Hash, class _Equal, class _Alloc>
1675template <class _InputIterator>
1676void
1677__hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_multi(_InputIterator __first,
1678 _InputIterator __last)
1679{
Eric Fiselierfcd02212016-02-11 11:59:44 +00001680 typedef iterator_traits<_InputIterator> _ITraits;
1681 typedef typename _ITraits::value_type _ItValueType;
1682 static_assert((is_same<_ItValueType, __container_value_type>::value ||
1683 is_same<_ItValueType, __node_value_type>::value),
1684 "__assign_multi may only be called with the containers value type"
1685 " or the nodes value type");
Howard Hinnant3e519522010-05-11 19:42:16 +00001686 if (bucket_count() != 0)
1687 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001688 __next_pointer __cache = __detach();
Howard Hinnant3e519522010-05-11 19:42:16 +00001689#ifndef _LIBCPP_NO_EXCEPTIONS
1690 try
1691 {
Howard Hinnantb3371f62010-08-22 00:02:43 +00001692#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001693 for (; __cache != nullptr && __first != __last; ++__first)
1694 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001695 __cache->__upcast()->__value_ = *__first;
1696 __next_pointer __next = __cache->__next_;
1697 __node_insert_multi(__cache->__upcast());
Howard Hinnant3e519522010-05-11 19:42:16 +00001698 __cache = __next;
1699 }
1700#ifndef _LIBCPP_NO_EXCEPTIONS
1701 }
1702 catch (...)
1703 {
1704 __deallocate(__cache);
1705 throw;
1706 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00001707#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001708 __deallocate(__cache);
1709 }
1710 for (; __first != __last; ++__first)
Eric Fiselierfcd02212016-02-11 11:59:44 +00001711 __insert_multi(_NodeTypes::__get_value(*__first));
Howard Hinnant3e519522010-05-11 19:42:16 +00001712}
1713
1714template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001715inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001716typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
Howard Hinnant37141072011-06-04 18:54:24 +00001717__hash_table<_Tp, _Hash, _Equal, _Alloc>::begin() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001718{
Howard Hinnantb24c8022013-07-23 22:01:58 +00001719#if _LIBCPP_DEBUG_LEVEL >= 2
1720 return iterator(__p1_.first().__next_, this);
1721#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001722 return iterator(__p1_.first().__next_);
Howard Hinnantb24c8022013-07-23 22:01:58 +00001723#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001724}
1725
1726template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001727inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001728typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
Howard Hinnant37141072011-06-04 18:54:24 +00001729__hash_table<_Tp, _Hash, _Equal, _Alloc>::end() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001730{
Howard Hinnantb24c8022013-07-23 22:01:58 +00001731#if _LIBCPP_DEBUG_LEVEL >= 2
1732 return iterator(nullptr, this);
1733#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001734 return iterator(nullptr);
Howard Hinnantb24c8022013-07-23 22:01:58 +00001735#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001736}
1737
1738template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001739inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001740typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
Howard Hinnant37141072011-06-04 18:54:24 +00001741__hash_table<_Tp, _Hash, _Equal, _Alloc>::begin() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001742{
Howard Hinnantb24c8022013-07-23 22:01:58 +00001743#if _LIBCPP_DEBUG_LEVEL >= 2
1744 return const_iterator(__p1_.first().__next_, this);
1745#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001746 return const_iterator(__p1_.first().__next_);
Howard Hinnantb24c8022013-07-23 22:01:58 +00001747#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001748}
1749
1750template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001751inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001752typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
Howard Hinnant37141072011-06-04 18:54:24 +00001753__hash_table<_Tp, _Hash, _Equal, _Alloc>::end() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001754{
Howard Hinnantb24c8022013-07-23 22:01:58 +00001755#if _LIBCPP_DEBUG_LEVEL >= 2
1756 return const_iterator(nullptr, this);
1757#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001758 return const_iterator(nullptr);
Howard Hinnantb24c8022013-07-23 22:01:58 +00001759#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001760}
1761
1762template <class _Tp, class _Hash, class _Equal, class _Alloc>
1763void
Howard Hinnant37141072011-06-04 18:54:24 +00001764__hash_table<_Tp, _Hash, _Equal, _Alloc>::clear() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001765{
1766 if (size() > 0)
1767 {
1768 __deallocate(__p1_.first().__next_);
1769 __p1_.first().__next_ = nullptr;
1770 size_type __bc = bucket_count();
Howard Hinnant1f8da842011-07-05 14:14:17 +00001771 for (size_type __i = 0; __i < __bc; ++__i)
Howard Hinnant3e519522010-05-11 19:42:16 +00001772 __bucket_list_[__i] = nullptr;
1773 size() = 0;
1774 }
1775}
1776
1777template <class _Tp, class _Hash, class _Equal, class _Alloc>
1778pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
1779__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_unique(__node_pointer __nd)
1780{
1781 __nd->__hash_ = hash_function()(__nd->__value_);
1782 size_type __bc = bucket_count();
1783 bool __inserted = false;
Eric Fiselier40492ba2016-07-23 20:36:55 +00001784 __next_pointer __ndptr;
Howard Hinnant3e519522010-05-11 19:42:16 +00001785 size_t __chash;
1786 if (__bc != 0)
1787 {
Howard Hinnant4cb38a82012-07-06 17:31:14 +00001788 __chash = __constrain_hash(__nd->__hash_, __bc);
Howard Hinnant3e519522010-05-11 19:42:16 +00001789 __ndptr = __bucket_list_[__chash];
1790 if (__ndptr != nullptr)
1791 {
1792 for (__ndptr = __ndptr->__next_; __ndptr != nullptr &&
Eric Fiselier40492ba2016-07-23 20:36:55 +00001793 __constrain_hash(__ndptr->__hash(), __bc) == __chash;
Howard Hinnant3e519522010-05-11 19:42:16 +00001794 __ndptr = __ndptr->__next_)
1795 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001796 if (key_eq()(__ndptr->__upcast()->__value_, __nd->__value_))
Howard Hinnant3e519522010-05-11 19:42:16 +00001797 goto __done;
1798 }
1799 }
1800 }
1801 {
1802 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1803 {
Eric Fiselier9ba5c112015-02-02 21:31:48 +00001804 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnant3e519522010-05-11 19:42:16 +00001805 size_type(ceil(float(size() + 1) / max_load_factor()))));
1806 __bc = bucket_count();
Howard Hinnant4cb38a82012-07-06 17:31:14 +00001807 __chash = __constrain_hash(__nd->__hash_, __bc);
Howard Hinnant3e519522010-05-11 19:42:16 +00001808 }
1809 // insert_after __bucket_list_[__chash], or __first_node if bucket is null
Eric Fiselier40492ba2016-07-23 20:36:55 +00001810 __next_pointer __pn = __bucket_list_[__chash];
Howard Hinnant3e519522010-05-11 19:42:16 +00001811 if (__pn == nullptr)
1812 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001813 __pn =__p1_.first().__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001814 __nd->__next_ = __pn->__next_;
Eric Fiselier40492ba2016-07-23 20:36:55 +00001815 __pn->__next_ = __nd->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001816 // fix up __bucket_list_
1817 __bucket_list_[__chash] = __pn;
1818 if (__nd->__next_ != nullptr)
Eric Fiselier40492ba2016-07-23 20:36:55 +00001819 __bucket_list_[__constrain_hash(__nd->__next_->__hash(), __bc)] = __nd->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001820 }
1821 else
1822 {
1823 __nd->__next_ = __pn->__next_;
Eric Fiselier40492ba2016-07-23 20:36:55 +00001824 __pn->__next_ = __nd->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001825 }
Eric Fiselier40492ba2016-07-23 20:36:55 +00001826 __ndptr = __nd->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001827 // increment size
1828 ++size();
1829 __inserted = true;
1830 }
1831__done:
Howard Hinnantb24c8022013-07-23 22:01:58 +00001832#if _LIBCPP_DEBUG_LEVEL >= 2
1833 return pair<iterator, bool>(iterator(__ndptr, this), __inserted);
1834#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001835 return pair<iterator, bool>(iterator(__ndptr), __inserted);
Howard Hinnantb24c8022013-07-23 22:01:58 +00001836#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001837}
1838
1839template <class _Tp, class _Hash, class _Equal, class _Alloc>
1840typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1841__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi(__node_pointer __cp)
1842{
1843 __cp->__hash_ = hash_function()(__cp->__value_);
1844 size_type __bc = bucket_count();
1845 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1846 {
Eric Fiselier9ba5c112015-02-02 21:31:48 +00001847 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnant3e519522010-05-11 19:42:16 +00001848 size_type(ceil(float(size() + 1) / max_load_factor()))));
1849 __bc = bucket_count();
1850 }
Howard Hinnant4cb38a82012-07-06 17:31:14 +00001851 size_t __chash = __constrain_hash(__cp->__hash_, __bc);
Eric Fiselier40492ba2016-07-23 20:36:55 +00001852 __next_pointer __pn = __bucket_list_[__chash];
Howard Hinnant3e519522010-05-11 19:42:16 +00001853 if (__pn == nullptr)
1854 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001855 __pn =__p1_.first().__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001856 __cp->__next_ = __pn->__next_;
Eric Fiselier40492ba2016-07-23 20:36:55 +00001857 __pn->__next_ = __cp->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001858 // fix up __bucket_list_
1859 __bucket_list_[__chash] = __pn;
1860 if (__cp->__next_ != nullptr)
Eric Fiselier40492ba2016-07-23 20:36:55 +00001861 __bucket_list_[__constrain_hash(__cp->__next_->__hash(), __bc)]
1862 = __cp->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001863 }
1864 else
1865 {
1866 for (bool __found = false; __pn->__next_ != nullptr &&
Eric Fiselier40492ba2016-07-23 20:36:55 +00001867 __constrain_hash(__pn->__next_->__hash(), __bc) == __chash;
Howard Hinnant3e519522010-05-11 19:42:16 +00001868 __pn = __pn->__next_)
Howard Hinnantb3371f62010-08-22 00:02:43 +00001869 {
Howard Hinnant3e519522010-05-11 19:42:16 +00001870 // __found key_eq() action
1871 // false false loop
1872 // true true loop
1873 // false true set __found to true
1874 // true false break
Eric Fiselier40492ba2016-07-23 20:36:55 +00001875 if (__found != (__pn->__next_->__hash() == __cp->__hash_ &&
1876 key_eq()(__pn->__next_->__upcast()->__value_, __cp->__value_)))
Howard Hinnant3e519522010-05-11 19:42:16 +00001877 {
1878 if (!__found)
1879 __found = true;
1880 else
1881 break;
1882 }
1883 }
1884 __cp->__next_ = __pn->__next_;
Eric Fiselier40492ba2016-07-23 20:36:55 +00001885 __pn->__next_ = __cp->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001886 if (__cp->__next_ != nullptr)
1887 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001888 size_t __nhash = __constrain_hash(__cp->__next_->__hash(), __bc);
Howard Hinnant3e519522010-05-11 19:42:16 +00001889 if (__nhash != __chash)
Eric Fiselier40492ba2016-07-23 20:36:55 +00001890 __bucket_list_[__nhash] = __cp->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001891 }
1892 }
1893 ++size();
Howard Hinnantb24c8022013-07-23 22:01:58 +00001894#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselier40492ba2016-07-23 20:36:55 +00001895 return iterator(__cp->__ptr(), this);
Howard Hinnantb24c8022013-07-23 22:01:58 +00001896#else
Eric Fiselier40492ba2016-07-23 20:36:55 +00001897 return iterator(__cp->__ptr());
Howard Hinnantb24c8022013-07-23 22:01:58 +00001898#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001899}
1900
1901template <class _Tp, class _Hash, class _Equal, class _Alloc>
1902typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1903__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi(
1904 const_iterator __p, __node_pointer __cp)
1905{
Howard Hinnant2f51de52013-08-02 17:50:49 +00001906#if _LIBCPP_DEBUG_LEVEL >= 2
1907 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1908 "unordered container::emplace_hint(const_iterator, args...) called with an iterator not"
1909 " referring to this unordered container");
1910#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001911 if (__p != end() && key_eq()(*__p, __cp->__value_))
1912 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001913 __next_pointer __np = __p.__node_;
1914 __cp->__hash_ = __np->__hash();
Howard Hinnant3e519522010-05-11 19:42:16 +00001915 size_type __bc = bucket_count();
1916 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1917 {
Eric Fiselier9ba5c112015-02-02 21:31:48 +00001918 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnant3e519522010-05-11 19:42:16 +00001919 size_type(ceil(float(size() + 1) / max_load_factor()))));
1920 __bc = bucket_count();
1921 }
Howard Hinnant4cb38a82012-07-06 17:31:14 +00001922 size_t __chash = __constrain_hash(__cp->__hash_, __bc);
Eric Fiselier40492ba2016-07-23 20:36:55 +00001923 __next_pointer __pp = __bucket_list_[__chash];
Howard Hinnant3e519522010-05-11 19:42:16 +00001924 while (__pp->__next_ != __np)
1925 __pp = __pp->__next_;
1926 __cp->__next_ = __np;
Eric Fiselier40492ba2016-07-23 20:36:55 +00001927 __pp->__next_ = static_cast<__next_pointer>(__cp);
Howard Hinnant3e519522010-05-11 19:42:16 +00001928 ++size();
Howard Hinnantb24c8022013-07-23 22:01:58 +00001929#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselier40492ba2016-07-23 20:36:55 +00001930 return iterator(static_cast<__next_pointer>(__cp), this);
Howard Hinnantb24c8022013-07-23 22:01:58 +00001931#else
Eric Fiselier40492ba2016-07-23 20:36:55 +00001932 return iterator(static_cast<__next_pointer>(__cp));
Howard Hinnantb24c8022013-07-23 22:01:58 +00001933#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001934 }
1935 return __node_insert_multi(__cp);
1936}
1937
Eric Fiselierde3f2b32015-06-13 07:18:32 +00001938
1939
Eric Fiselierfcd02212016-02-11 11:59:44 +00001940#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierde3f2b32015-06-13 07:18:32 +00001941template <class _Tp, class _Hash, class _Equal, class _Alloc>
Eric Fiselierfcd02212016-02-11 11:59:44 +00001942template <class _Key, class ..._Args>
Eric Fiselierde3f2b32015-06-13 07:18:32 +00001943pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
Eric Fiselierfcd02212016-02-11 11:59:44 +00001944__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_key_args(_Key const& __k, _Args&&... __args)
Eric Fiselierde3f2b32015-06-13 07:18:32 +00001945#else
1946template <class _Tp, class _Hash, class _Equal, class _Alloc>
Eric Fiselierfcd02212016-02-11 11:59:44 +00001947template <class _Key, class _Args>
Eric Fiselierde3f2b32015-06-13 07:18:32 +00001948pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
Eric Fiselierfcd02212016-02-11 11:59:44 +00001949__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_key_args(_Key const& __k, _Args& __args)
Eric Fiselierde3f2b32015-06-13 07:18:32 +00001950#endif
1951{
Eric Fiselierfcd02212016-02-11 11:59:44 +00001952
1953 size_t __hash = hash_function()(__k);
Howard Hinnant3e519522010-05-11 19:42:16 +00001954 size_type __bc = bucket_count();
1955 bool __inserted = false;
Eric Fiselier40492ba2016-07-23 20:36:55 +00001956 __next_pointer __nd;
Howard Hinnant3e519522010-05-11 19:42:16 +00001957 size_t __chash;
1958 if (__bc != 0)
1959 {
Howard Hinnant4cb38a82012-07-06 17:31:14 +00001960 __chash = __constrain_hash(__hash, __bc);
Howard Hinnant3e519522010-05-11 19:42:16 +00001961 __nd = __bucket_list_[__chash];
1962 if (__nd != nullptr)
1963 {
1964 for (__nd = __nd->__next_; __nd != nullptr &&
Eric Fiselier1a06fe52016-07-24 06:22:25 +00001965 (__nd->__hash() == __hash || __constrain_hash(__nd->__hash(), __bc) == __chash);
Howard Hinnant3e519522010-05-11 19:42:16 +00001966 __nd = __nd->__next_)
1967 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001968 if (key_eq()(__nd->__upcast()->__value_, __k))
Howard Hinnant3e519522010-05-11 19:42:16 +00001969 goto __done;
1970 }
1971 }
1972 }
1973 {
Eric Fiselierfcd02212016-02-11 11:59:44 +00001974#ifndef _LIBCPP_CXX03_LANG
1975 __node_holder __h = __construct_node_hash(__hash, _VSTD::forward<_Args>(__args)...);
1976#else
1977 __node_holder __h = __construct_node_hash(__hash, __args);
1978#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001979 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1980 {
Eric Fiselier9ba5c112015-02-02 21:31:48 +00001981 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnant3e519522010-05-11 19:42:16 +00001982 size_type(ceil(float(size() + 1) / max_load_factor()))));
1983 __bc = bucket_count();
Howard Hinnant4cb38a82012-07-06 17:31:14 +00001984 __chash = __constrain_hash(__hash, __bc);
Howard Hinnant3e519522010-05-11 19:42:16 +00001985 }
1986 // insert_after __bucket_list_[__chash], or __first_node if bucket is null
Eric Fiselier40492ba2016-07-23 20:36:55 +00001987 __next_pointer __pn = __bucket_list_[__chash];
Howard Hinnant3e519522010-05-11 19:42:16 +00001988 if (__pn == nullptr)
1989 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001990 __pn = __p1_.first().__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001991 __h->__next_ = __pn->__next_;
Eric Fiselier40492ba2016-07-23 20:36:55 +00001992 __pn->__next_ = __h.get()->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001993 // fix up __bucket_list_
1994 __bucket_list_[__chash] = __pn;
1995 if (__h->__next_ != nullptr)
Eric Fiselier40492ba2016-07-23 20:36:55 +00001996 __bucket_list_[__constrain_hash(__h->__next_->__hash(), __bc)]
1997 = __h.get()->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001998 }
1999 else
2000 {
2001 __h->__next_ = __pn->__next_;
Eric Fiselier40492ba2016-07-23 20:36:55 +00002002 __pn->__next_ = static_cast<__next_pointer>(__h.get());
Howard Hinnant3e519522010-05-11 19:42:16 +00002003 }
Eric Fiselier40492ba2016-07-23 20:36:55 +00002004 __nd = static_cast<__next_pointer>(__h.release());
Howard Hinnant3e519522010-05-11 19:42:16 +00002005 // increment size
2006 ++size();
2007 __inserted = true;
2008 }
2009__done:
Howard Hinnantb24c8022013-07-23 22:01:58 +00002010#if _LIBCPP_DEBUG_LEVEL >= 2
2011 return pair<iterator, bool>(iterator(__nd, this), __inserted);
2012#else
Howard Hinnant3e519522010-05-11 19:42:16 +00002013 return pair<iterator, bool>(iterator(__nd), __inserted);
Howard Hinnantb24c8022013-07-23 22:01:58 +00002014#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002015}
2016
Eric Fiselierfcd02212016-02-11 11:59:44 +00002017#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00002018
2019template <class _Tp, class _Hash, class _Equal, class _Alloc>
2020template <class... _Args>
2021pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
Duncan P. N. Exon Smithfde79b42016-03-17 20:45:20 +00002022__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_impl(_Args&&... __args)
Howard Hinnant3e519522010-05-11 19:42:16 +00002023{
Howard Hinnantce48a112011-06-30 21:18:19 +00002024 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnant3e519522010-05-11 19:42:16 +00002025 pair<iterator, bool> __r = __node_insert_unique(__h.get());
2026 if (__r.second)
2027 __h.release();
2028 return __r;
2029}
2030
2031template <class _Tp, class _Hash, class _Equal, class _Alloc>
2032template <class... _Args>
2033typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2034__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_multi(_Args&&... __args)
2035{
Howard Hinnantce48a112011-06-30 21:18:19 +00002036 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnant3e519522010-05-11 19:42:16 +00002037 iterator __r = __node_insert_multi(__h.get());
2038 __h.release();
2039 return __r;
2040}
2041
2042template <class _Tp, class _Hash, class _Equal, class _Alloc>
2043template <class... _Args>
2044typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2045__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_hint_multi(
2046 const_iterator __p, _Args&&... __args)
2047{
Howard Hinnante5c13de2013-07-29 19:05:47 +00002048#if _LIBCPP_DEBUG_LEVEL >= 2
2049 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2050 "unordered container::emplace_hint(const_iterator, args...) called with an iterator not"
2051 " referring to this unordered container");
2052#endif
Howard Hinnantce48a112011-06-30 21:18:19 +00002053 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnant3e519522010-05-11 19:42:16 +00002054 iterator __r = __node_insert_multi(__p, __h.get());
2055 __h.release();
2056 return __r;
2057}
2058
Eric Fiselierfcd02212016-02-11 11:59:44 +00002059#else // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00002060
2061template <class _Tp, class _Hash, class _Equal, class _Alloc>
2062typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
Eric Fiselierfcd02212016-02-11 11:59:44 +00002063__hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const __container_value_type& __x)
Howard Hinnant3e519522010-05-11 19:42:16 +00002064{
2065 __node_holder __h = __construct_node(__x);
2066 iterator __r = __node_insert_multi(__h.get());
2067 __h.release();
2068 return __r;
2069}
2070
2071template <class _Tp, class _Hash, class _Equal, class _Alloc>
2072typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2073__hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const_iterator __p,
Eric Fiselierfcd02212016-02-11 11:59:44 +00002074 const __container_value_type& __x)
Howard Hinnant3e519522010-05-11 19:42:16 +00002075{
Howard Hinnante5c13de2013-07-29 19:05:47 +00002076#if _LIBCPP_DEBUG_LEVEL >= 2
2077 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2078 "unordered container::insert(const_iterator, lvalue) called with an iterator not"
2079 " referring to this unordered container");
2080#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002081 __node_holder __h = __construct_node(__x);
2082 iterator __r = __node_insert_multi(__p, __h.get());
2083 __h.release();
2084 return __r;
2085}
2086
Eric Fiselierfcd02212016-02-11 11:59:44 +00002087#endif // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00002088
2089template <class _Tp, class _Hash, class _Equal, class _Alloc>
2090void
2091__hash_table<_Tp, _Hash, _Equal, _Alloc>::rehash(size_type __n)
2092{
Howard Hinnant4cb38a82012-07-06 17:31:14 +00002093 if (__n == 1)
2094 __n = 2;
2095 else if (__n & (__n - 1))
2096 __n = __next_prime(__n);
Howard Hinnant3e519522010-05-11 19:42:16 +00002097 size_type __bc = bucket_count();
2098 if (__n > __bc)
2099 __rehash(__n);
Howard Hinnant4cb38a82012-07-06 17:31:14 +00002100 else if (__n < __bc)
Howard Hinnant3e519522010-05-11 19:42:16 +00002101 {
Howard Hinnantce48a112011-06-30 21:18:19 +00002102 __n = _VSTD::max<size_type>
Howard Hinnant3e519522010-05-11 19:42:16 +00002103 (
2104 __n,
Eric Fiselier9ba5c112015-02-02 21:31:48 +00002105 __is_hash_power2(__bc) ? __next_hash_pow2(size_t(ceil(float(size()) / max_load_factor()))) :
2106 __next_prime(size_t(ceil(float(size()) / max_load_factor())))
Howard Hinnant3e519522010-05-11 19:42:16 +00002107 );
2108 if (__n < __bc)
2109 __rehash(__n);
2110 }
2111}
2112
2113template <class _Tp, class _Hash, class _Equal, class _Alloc>
2114void
2115__hash_table<_Tp, _Hash, _Equal, _Alloc>::__rehash(size_type __nbc)
2116{
Howard Hinnantb24c8022013-07-23 22:01:58 +00002117#if _LIBCPP_DEBUG_LEVEL >= 2
2118 __get_db()->__invalidate_all(this);
2119#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant3e519522010-05-11 19:42:16 +00002120 __pointer_allocator& __npa = __bucket_list_.get_deleter().__alloc();
2121 __bucket_list_.reset(__nbc > 0 ?
2122 __pointer_alloc_traits::allocate(__npa, __nbc) : nullptr);
2123 __bucket_list_.get_deleter().size() = __nbc;
2124 if (__nbc > 0)
2125 {
2126 for (size_type __i = 0; __i < __nbc; ++__i)
2127 __bucket_list_[__i] = nullptr;
Eric Fiselier40492ba2016-07-23 20:36:55 +00002128 __next_pointer __pp = __p1_.first().__ptr();
2129 __next_pointer __cp = __pp->__next_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002130 if (__cp != nullptr)
2131 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00002132 size_type __chash = __constrain_hash(__cp->__hash(), __nbc);
Howard Hinnant3e519522010-05-11 19:42:16 +00002133 __bucket_list_[__chash] = __pp;
2134 size_type __phash = __chash;
2135 for (__pp = __cp, __cp = __cp->__next_; __cp != nullptr;
2136 __cp = __pp->__next_)
2137 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00002138 __chash = __constrain_hash(__cp->__hash(), __nbc);
Howard Hinnant3e519522010-05-11 19:42:16 +00002139 if (__chash == __phash)
2140 __pp = __cp;
2141 else
2142 {
2143 if (__bucket_list_[__chash] == nullptr)
2144 {
2145 __bucket_list_[__chash] = __pp;
2146 __pp = __cp;
2147 __phash = __chash;
2148 }
2149 else
2150 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00002151 __next_pointer __np = __cp;
Howard Hinnant3e519522010-05-11 19:42:16 +00002152 for (; __np->__next_ != nullptr &&
Eric Fiselier40492ba2016-07-23 20:36:55 +00002153 key_eq()(__cp->__upcast()->__value_,
2154 __np->__next_->__upcast()->__value_);
Howard Hinnant3e519522010-05-11 19:42:16 +00002155 __np = __np->__next_)
2156 ;
2157 __pp->__next_ = __np->__next_;
2158 __np->__next_ = __bucket_list_[__chash]->__next_;
2159 __bucket_list_[__chash]->__next_ = __cp;
Howard Hinnantb3371f62010-08-22 00:02:43 +00002160
Howard Hinnant3e519522010-05-11 19:42:16 +00002161 }
2162 }
2163 }
2164 }
2165 }
2166}
2167
2168template <class _Tp, class _Hash, class _Equal, class _Alloc>
2169template <class _Key>
2170typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2171__hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k)
2172{
2173 size_t __hash = hash_function()(__k);
2174 size_type __bc = bucket_count();
2175 if (__bc != 0)
2176 {
Howard Hinnant4cb38a82012-07-06 17:31:14 +00002177 size_t __chash = __constrain_hash(__hash, __bc);
Eric Fiselier40492ba2016-07-23 20:36:55 +00002178 __next_pointer __nd = __bucket_list_[__chash];
Howard Hinnant3e519522010-05-11 19:42:16 +00002179 if (__nd != nullptr)
2180 {
2181 for (__nd = __nd->__next_; __nd != nullptr &&
Eric Fiselier40492ba2016-07-23 20:36:55 +00002182 (__nd->__hash() == __hash
2183 || __constrain_hash(__nd->__hash(), __bc) == __chash);
Howard Hinnant3e519522010-05-11 19:42:16 +00002184 __nd = __nd->__next_)
2185 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00002186 if ((__nd->__hash() == __hash)
2187 && key_eq()(__nd->__upcast()->__value_, __k))
Howard Hinnantb24c8022013-07-23 22:01:58 +00002188#if _LIBCPP_DEBUG_LEVEL >= 2
2189 return iterator(__nd, this);
2190#else
Howard Hinnant3e519522010-05-11 19:42:16 +00002191 return iterator(__nd);
Howard Hinnantb24c8022013-07-23 22:01:58 +00002192#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002193 }
2194 }
2195 }
2196 return end();
2197}
2198
2199template <class _Tp, class _Hash, class _Equal, class _Alloc>
2200template <class _Key>
2201typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
2202__hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k) const
2203{
2204 size_t __hash = hash_function()(__k);
2205 size_type __bc = bucket_count();
2206 if (__bc != 0)
2207 {
Howard Hinnant4cb38a82012-07-06 17:31:14 +00002208 size_t __chash = __constrain_hash(__hash, __bc);
Eric Fiselier40492ba2016-07-23 20:36:55 +00002209 __next_pointer __nd = __bucket_list_[__chash];
Howard Hinnant3e519522010-05-11 19:42:16 +00002210 if (__nd != nullptr)
2211 {
2212 for (__nd = __nd->__next_; __nd != nullptr &&
Eric Fiselier40492ba2016-07-23 20:36:55 +00002213 (__hash == __nd->__hash()
2214 || __constrain_hash(__nd->__hash(), __bc) == __chash);
Howard Hinnant3e519522010-05-11 19:42:16 +00002215 __nd = __nd->__next_)
2216 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00002217 if ((__nd->__hash() == __hash)
2218 && key_eq()(__nd->__upcast()->__value_, __k))
Howard Hinnantb24c8022013-07-23 22:01:58 +00002219#if _LIBCPP_DEBUG_LEVEL >= 2
2220 return const_iterator(__nd, this);
2221#else
Howard Hinnant3e519522010-05-11 19:42:16 +00002222 return const_iterator(__nd);
Howard Hinnantb24c8022013-07-23 22:01:58 +00002223#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002224 }
2225 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00002226
Howard Hinnant3e519522010-05-11 19:42:16 +00002227 }
2228 return end();
2229}
2230
Eric Fiselierfcd02212016-02-11 11:59:44 +00002231#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00002232
2233template <class _Tp, class _Hash, class _Equal, class _Alloc>
2234template <class ..._Args>
2235typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
2236__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(_Args&& ...__args)
2237{
Eric Fiselierfcd02212016-02-11 11:59:44 +00002238 static_assert(!__is_hash_value_type<_Args...>::value,
2239 "Construct cannot be called with a hash value type");
Howard Hinnant3e519522010-05-11 19:42:16 +00002240 __node_allocator& __na = __node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00002241 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselierfcd02212016-02-11 11:59:44 +00002242 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), _VSTD::forward<_Args>(__args)...);
Howard Hinnant3e519522010-05-11 19:42:16 +00002243 __h.get_deleter().__value_constructed = true;
2244 __h->__hash_ = hash_function()(__h->__value_);
2245 __h->__next_ = nullptr;
2246 return __h;
2247}
2248
2249template <class _Tp, class _Hash, class _Equal, class _Alloc>
Eric Fiselierfcd02212016-02-11 11:59:44 +00002250template <class _First, class ..._Rest>
Howard Hinnant3e519522010-05-11 19:42:16 +00002251typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Eric Fiselierfcd02212016-02-11 11:59:44 +00002252__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node_hash(
2253 size_t __hash, _First&& __f, _Rest&& ...__rest)
Howard Hinnant3e519522010-05-11 19:42:16 +00002254{
Eric Fiselierfcd02212016-02-11 11:59:44 +00002255 static_assert(!__is_hash_value_type<_First, _Rest...>::value,
2256 "Construct cannot be called with a hash value type");
Howard Hinnant3e519522010-05-11 19:42:16 +00002257 __node_allocator& __na = __node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00002258 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselierfcd02212016-02-11 11:59:44 +00002259 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_),
2260 _VSTD::forward<_First>(__f),
2261 _VSTD::forward<_Rest>(__rest)...);
Howard Hinnant3e519522010-05-11 19:42:16 +00002262 __h.get_deleter().__value_constructed = true;
2263 __h->__hash_ = __hash;
2264 __h->__next_ = nullptr;
Howard Hinnant179b1f82013-08-22 18:29:50 +00002265 return __h;
Howard Hinnant3e519522010-05-11 19:42:16 +00002266}
2267
Eric Fiselierfcd02212016-02-11 11:59:44 +00002268#else // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00002269
2270template <class _Tp, class _Hash, class _Equal, class _Alloc>
2271typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Eric Fiselierfcd02212016-02-11 11:59:44 +00002272__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(const __container_value_type& __v)
Howard Hinnant3e519522010-05-11 19:42:16 +00002273{
2274 __node_allocator& __na = __node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00002275 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselierfcd02212016-02-11 11:59:44 +00002276 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), __v);
Howard Hinnant3e519522010-05-11 19:42:16 +00002277 __h.get_deleter().__value_constructed = true;
2278 __h->__hash_ = hash_function()(__h->__value_);
2279 __h->__next_ = nullptr;
Dimitry Andric251c6292015-08-19 06:43:33 +00002280 return _LIBCPP_EXPLICIT_MOVE(__h); // explicitly moved for C++03
Howard Hinnant3e519522010-05-11 19:42:16 +00002281}
2282
Howard Hinnant3e519522010-05-11 19:42:16 +00002283template <class _Tp, class _Hash, class _Equal, class _Alloc>
2284typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Eric Fiselierfcd02212016-02-11 11:59:44 +00002285__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node_hash(size_t __hash,
2286 const __container_value_type& __v)
Howard Hinnant3e519522010-05-11 19:42:16 +00002287{
2288 __node_allocator& __na = __node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00002289 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselierfcd02212016-02-11 11:59:44 +00002290 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), __v);
Howard Hinnant3e519522010-05-11 19:42:16 +00002291 __h.get_deleter().__value_constructed = true;
2292 __h->__hash_ = __hash;
2293 __h->__next_ = nullptr;
Dimitry Andric251c6292015-08-19 06:43:33 +00002294 return _LIBCPP_EXPLICIT_MOVE(__h); // explicitly moved for C++03
Howard Hinnant3e519522010-05-11 19:42:16 +00002295}
2296
Eric Fiselierfcd02212016-02-11 11:59:44 +00002297#endif // _LIBCPP_CXX03_LANG
2298
Howard Hinnant3e519522010-05-11 19:42:16 +00002299template <class _Tp, class _Hash, class _Equal, class _Alloc>
2300typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2301__hash_table<_Tp, _Hash, _Equal, _Alloc>::erase(const_iterator __p)
2302{
Eric Fiselier40492ba2016-07-23 20:36:55 +00002303 __next_pointer __np = __p.__node_;
Howard Hinnantb24c8022013-07-23 22:01:58 +00002304#if _LIBCPP_DEBUG_LEVEL >= 2
2305 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2306 "unordered container erase(iterator) called with an iterator not"
2307 " referring to this container");
2308 _LIBCPP_ASSERT(__p != end(),
2309 "unordered container erase(iterator) called with a non-dereferenceable iterator");
2310 iterator __r(__np, this);
2311#else
Howard Hinnant3e519522010-05-11 19:42:16 +00002312 iterator __r(__np);
Howard Hinnantb24c8022013-07-23 22:01:58 +00002313#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002314 ++__r;
2315 remove(__p);
2316 return __r;
2317}
2318
2319template <class _Tp, class _Hash, class _Equal, class _Alloc>
2320typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2321__hash_table<_Tp, _Hash, _Equal, _Alloc>::erase(const_iterator __first,
2322 const_iterator __last)
2323{
Howard Hinnantb24c8022013-07-23 22:01:58 +00002324#if _LIBCPP_DEBUG_LEVEL >= 2
2325 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
2326 "unodered container::erase(iterator, iterator) called with an iterator not"
2327 " referring to this unodered container");
2328 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__last) == this,
2329 "unodered container::erase(iterator, iterator) called with an iterator not"
2330 " referring to this unodered container");
2331#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002332 for (const_iterator __p = __first; __first != __last; __p = __first)
2333 {
2334 ++__first;
2335 erase(__p);
2336 }
Eric Fiselier40492ba2016-07-23 20:36:55 +00002337 __next_pointer __np = __last.__node_;
Howard Hinnantb24c8022013-07-23 22:01:58 +00002338#if _LIBCPP_DEBUG_LEVEL >= 2
2339 return iterator (__np, this);
2340#else
Howard Hinnant3e519522010-05-11 19:42:16 +00002341 return iterator (__np);
Howard Hinnantb24c8022013-07-23 22:01:58 +00002342#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002343}
2344
2345template <class _Tp, class _Hash, class _Equal, class _Alloc>
2346template <class _Key>
2347typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2348__hash_table<_Tp, _Hash, _Equal, _Alloc>::__erase_unique(const _Key& __k)
2349{
2350 iterator __i = find(__k);
2351 if (__i == end())
2352 return 0;
2353 erase(__i);
2354 return 1;
2355}
2356
2357template <class _Tp, class _Hash, class _Equal, class _Alloc>
2358template <class _Key>
2359typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2360__hash_table<_Tp, _Hash, _Equal, _Alloc>::__erase_multi(const _Key& __k)
2361{
2362 size_type __r = 0;
2363 iterator __i = find(__k);
2364 if (__i != end())
2365 {
2366 iterator __e = end();
2367 do
2368 {
2369 erase(__i++);
2370 ++__r;
2371 } while (__i != __e && key_eq()(*__i, __k));
2372 }
2373 return __r;
2374}
2375
2376template <class _Tp, class _Hash, class _Equal, class _Alloc>
2377typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Howard Hinnant37141072011-06-04 18:54:24 +00002378__hash_table<_Tp, _Hash, _Equal, _Alloc>::remove(const_iterator __p) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00002379{
2380 // current node
Eric Fiselier40492ba2016-07-23 20:36:55 +00002381 __next_pointer __cn = __p.__node_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002382 size_type __bc = bucket_count();
Eric Fiselier40492ba2016-07-23 20:36:55 +00002383 size_t __chash = __constrain_hash(__cn->__hash(), __bc);
Howard Hinnant3e519522010-05-11 19:42:16 +00002384 // find previous node
Eric Fiselier40492ba2016-07-23 20:36:55 +00002385 __next_pointer __pn = __bucket_list_[__chash];
Howard Hinnant3e519522010-05-11 19:42:16 +00002386 for (; __pn->__next_ != __cn; __pn = __pn->__next_)
2387 ;
2388 // Fix up __bucket_list_
2389 // if __pn is not in same bucket (before begin is not in same bucket) &&
2390 // if __cn->__next_ is not in same bucket (nullptr is not in same bucket)
Eric Fiselier40492ba2016-07-23 20:36:55 +00002391 if (__pn == __p1_.first().__ptr()
2392 || __constrain_hash(__pn->__hash(), __bc) != __chash)
Howard Hinnant3e519522010-05-11 19:42:16 +00002393 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00002394 if (__cn->__next_ == nullptr
2395 || __constrain_hash(__cn->__next_->__hash(), __bc) != __chash)
Howard Hinnant3e519522010-05-11 19:42:16 +00002396 __bucket_list_[__chash] = nullptr;
2397 }
2398 // if __cn->__next_ is not in same bucket (nullptr is in same bucket)
2399 if (__cn->__next_ != nullptr)
2400 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00002401 size_t __nhash = __constrain_hash(__cn->__next_->__hash(), __bc);
Howard Hinnant3e519522010-05-11 19:42:16 +00002402 if (__nhash != __chash)
2403 __bucket_list_[__nhash] = __pn;
2404 }
2405 // remove __cn
2406 __pn->__next_ = __cn->__next_;
2407 __cn->__next_ = nullptr;
2408 --size();
Howard Hinnantb24c8022013-07-23 22:01:58 +00002409#if _LIBCPP_DEBUG_LEVEL >= 2
2410 __c_node* __c = __get_db()->__find_c_and_lock(this);
Eric Fiselier780b51d2016-12-28 05:53:01 +00002411 for (__i_node** __dp = __c->end_; __dp != __c->beg_; )
Howard Hinnantb24c8022013-07-23 22:01:58 +00002412 {
Eric Fiselier780b51d2016-12-28 05:53:01 +00002413 --__dp;
2414 iterator* __i = static_cast<iterator*>((*__dp)->__i_);
Howard Hinnantb24c8022013-07-23 22:01:58 +00002415 if (__i->__node_ == __cn)
2416 {
Eric Fiselier780b51d2016-12-28 05:53:01 +00002417 (*__dp)->__c_ = nullptr;
2418 if (--__c->end_ != __dp)
2419 memmove(__dp, __dp+1, (__c->end_ - __dp)*sizeof(__i_node*));
Howard Hinnantb24c8022013-07-23 22:01:58 +00002420 }
2421 }
2422 __get_db()->unlock();
2423#endif
Eric Fiselier40492ba2016-07-23 20:36:55 +00002424 return __node_holder(__cn->__upcast(), _Dp(__node_alloc(), true));
Howard Hinnant3e519522010-05-11 19:42:16 +00002425}
2426
2427template <class _Tp, class _Hash, class _Equal, class _Alloc>
2428template <class _Key>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00002429inline
Howard Hinnant3e519522010-05-11 19:42:16 +00002430typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2431__hash_table<_Tp, _Hash, _Equal, _Alloc>::__count_unique(const _Key& __k) const
2432{
2433 return static_cast<size_type>(find(__k) != end());
2434}
2435
2436template <class _Tp, class _Hash, class _Equal, class _Alloc>
2437template <class _Key>
2438typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2439__hash_table<_Tp, _Hash, _Equal, _Alloc>::__count_multi(const _Key& __k) const
2440{
2441 size_type __r = 0;
2442 const_iterator __i = find(__k);
2443 if (__i != end())
2444 {
2445 const_iterator __e = end();
2446 do
2447 {
2448 ++__i;
2449 ++__r;
2450 } while (__i != __e && key_eq()(*__i, __k));
2451 }
2452 return __r;
2453}
2454
2455template <class _Tp, class _Hash, class _Equal, class _Alloc>
2456template <class _Key>
2457pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator,
2458 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator>
2459__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_unique(
2460 const _Key& __k)
2461{
2462 iterator __i = find(__k);
2463 iterator __j = __i;
2464 if (__i != end())
2465 ++__j;
2466 return pair<iterator, iterator>(__i, __j);
2467}
2468
2469template <class _Tp, class _Hash, class _Equal, class _Alloc>
2470template <class _Key>
2471pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator,
2472 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator>
2473__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_unique(
2474 const _Key& __k) const
2475{
2476 const_iterator __i = find(__k);
2477 const_iterator __j = __i;
2478 if (__i != end())
2479 ++__j;
2480 return pair<const_iterator, const_iterator>(__i, __j);
2481}
2482
2483template <class _Tp, class _Hash, class _Equal, class _Alloc>
2484template <class _Key>
2485pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator,
2486 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator>
2487__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_multi(
2488 const _Key& __k)
2489{
2490 iterator __i = find(__k);
2491 iterator __j = __i;
2492 if (__i != end())
2493 {
2494 iterator __e = end();
2495 do
2496 {
2497 ++__j;
2498 } while (__j != __e && key_eq()(*__j, __k));
2499 }
2500 return pair<iterator, iterator>(__i, __j);
2501}
2502
2503template <class _Tp, class _Hash, class _Equal, class _Alloc>
2504template <class _Key>
2505pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator,
2506 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator>
2507__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_multi(
2508 const _Key& __k) const
2509{
2510 const_iterator __i = find(__k);
2511 const_iterator __j = __i;
2512 if (__i != end())
2513 {
2514 const_iterator __e = end();
2515 do
2516 {
2517 ++__j;
2518 } while (__j != __e && key_eq()(*__j, __k));
2519 }
2520 return pair<const_iterator, const_iterator>(__i, __j);
2521}
2522
2523template <class _Tp, class _Hash, class _Equal, class _Alloc>
2524void
2525__hash_table<_Tp, _Hash, _Equal, _Alloc>::swap(__hash_table& __u)
Eric Fiselier87a82492015-07-18 20:40:46 +00002526#if _LIBCPP_STD_VER <= 11
Eric Fiselier780b51d2016-12-28 05:53:01 +00002527 _NOEXCEPT_DEBUG_(
Marshall Clowe3fbe142015-07-13 20:04:56 +00002528 __is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value
Marshall Clowe3fbe142015-07-13 20:04:56 +00002529 && (!allocator_traits<__pointer_allocator>::propagate_on_container_swap::value
2530 || __is_nothrow_swappable<__pointer_allocator>::value)
2531 && (!__node_traits::propagate_on_container_swap::value
2532 || __is_nothrow_swappable<__node_allocator>::value)
Marshall Clowe3fbe142015-07-13 20:04:56 +00002533 )
Eric Fiselier87a82492015-07-18 20:40:46 +00002534#else
Eric Fiselier780b51d2016-12-28 05:53:01 +00002535 _NOEXCEPT_DEBUG_(__is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value)
Eric Fiselier87a82492015-07-18 20:40:46 +00002536#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002537{
Eric Fiselier780b51d2016-12-28 05:53:01 +00002538 _LIBCPP_ASSERT(__node_traits::propagate_on_container_swap::value ||
2539 this->__node_alloc() == __u.__node_alloc(),
2540 "list::swap: Either propagate_on_container_swap must be true"
2541 " or the allocators must compare equal");
Howard Hinnant3e519522010-05-11 19:42:16 +00002542 {
2543 __node_pointer_pointer __npp = __bucket_list_.release();
2544 __bucket_list_.reset(__u.__bucket_list_.release());
2545 __u.__bucket_list_.reset(__npp);
2546 }
Howard Hinnantce48a112011-06-30 21:18:19 +00002547 _VSTD::swap(__bucket_list_.get_deleter().size(), __u.__bucket_list_.get_deleter().size());
Marshall Clowe3fbe142015-07-13 20:04:56 +00002548 __swap_allocator(__bucket_list_.get_deleter().__alloc(),
Howard Hinnant3e519522010-05-11 19:42:16 +00002549 __u.__bucket_list_.get_deleter().__alloc());
Marshall Clowe3fbe142015-07-13 20:04:56 +00002550 __swap_allocator(__node_alloc(), __u.__node_alloc());
Howard Hinnantce48a112011-06-30 21:18:19 +00002551 _VSTD::swap(__p1_.first().__next_, __u.__p1_.first().__next_);
Howard Hinnant3e519522010-05-11 19:42:16 +00002552 __p2_.swap(__u.__p2_);
2553 __p3_.swap(__u.__p3_);
2554 if (size() > 0)
Eric Fiselier40492ba2016-07-23 20:36:55 +00002555 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash(), bucket_count())] =
2556 __p1_.first().__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00002557 if (__u.size() > 0)
Eric Fiselier40492ba2016-07-23 20:36:55 +00002558 __u.__bucket_list_[__constrain_hash(__u.__p1_.first().__next_->__hash(), __u.bucket_count())] =
2559 __u.__p1_.first().__ptr();
Howard Hinnantb24c8022013-07-23 22:01:58 +00002560#if _LIBCPP_DEBUG_LEVEL >= 2
2561 __get_db()->swap(this, &__u);
2562#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002563}
2564
2565template <class _Tp, class _Hash, class _Equal, class _Alloc>
2566typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2567__hash_table<_Tp, _Hash, _Equal, _Alloc>::bucket_size(size_type __n) const
2568{
Howard Hinnante5c13de2013-07-29 19:05:47 +00002569 _LIBCPP_ASSERT(__n < bucket_count(),
2570 "unordered container::bucket_size(n) called with n >= bucket_count()");
Eric Fiselier40492ba2016-07-23 20:36:55 +00002571 __next_pointer __np = __bucket_list_[__n];
Howard Hinnant3e519522010-05-11 19:42:16 +00002572 size_type __bc = bucket_count();
2573 size_type __r = 0;
2574 if (__np != nullptr)
2575 {
2576 for (__np = __np->__next_; __np != nullptr &&
Eric Fiselier40492ba2016-07-23 20:36:55 +00002577 __constrain_hash(__np->__hash(), __bc) == __n;
Howard Hinnant3e519522010-05-11 19:42:16 +00002578 __np = __np->__next_, ++__r)
2579 ;
2580 }
2581 return __r;
2582}
2583
Howard Hinnant37141072011-06-04 18:54:24 +00002584template <class _Tp, class _Hash, class _Equal, class _Alloc>
2585inline _LIBCPP_INLINE_VISIBILITY
2586void
2587swap(__hash_table<_Tp, _Hash, _Equal, _Alloc>& __x,
2588 __hash_table<_Tp, _Hash, _Equal, _Alloc>& __y)
2589 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
2590{
2591 __x.swap(__y);
2592}
2593
Howard Hinnantb24c8022013-07-23 22:01:58 +00002594#if _LIBCPP_DEBUG_LEVEL >= 2
2595
2596template <class _Tp, class _Hash, class _Equal, class _Alloc>
2597bool
2598__hash_table<_Tp, _Hash, _Equal, _Alloc>::__dereferenceable(const const_iterator* __i) const
2599{
2600 return __i->__node_ != nullptr;
2601}
2602
2603template <class _Tp, class _Hash, class _Equal, class _Alloc>
2604bool
2605__hash_table<_Tp, _Hash, _Equal, _Alloc>::__decrementable(const const_iterator*) const
2606{
2607 return false;
2608}
2609
2610template <class _Tp, class _Hash, class _Equal, class _Alloc>
2611bool
2612__hash_table<_Tp, _Hash, _Equal, _Alloc>::__addable(const const_iterator*, ptrdiff_t) const
2613{
2614 return false;
2615}
2616
2617template <class _Tp, class _Hash, class _Equal, class _Alloc>
2618bool
2619__hash_table<_Tp, _Hash, _Equal, _Alloc>::__subscriptable(const const_iterator*, ptrdiff_t) const
2620{
2621 return false;
2622}
2623
2624#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant3e519522010-05-11 19:42:16 +00002625_LIBCPP_END_NAMESPACE_STD
2626
2627#endif // _LIBCPP__HASH_TABLE