blob: 3072f93e00b9a8d06a2022bef9dc64a71ceefd56 [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 {
1007 return allocator_traits<__pointer_allocator>::max_size(
1008 __bucket_list_.get_deleter().__alloc());
1009 }
1010
1011 pair<iterator, bool> __node_insert_unique(__node_pointer __nd);
1012 iterator __node_insert_multi(__node_pointer __nd);
1013 iterator __node_insert_multi(const_iterator __p,
1014 __node_pointer __nd);
1015
Eric Fiselierfcd02212016-02-11 11:59:44 +00001016#ifndef _LIBCPP_CXX03_LANG
1017 template <class _Key, class ..._Args>
Duncan P. N. Exon Smithfde79b42016-03-17 20:45:20 +00001018 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierfcd02212016-02-11 11:59:44 +00001019 pair<iterator, bool> __emplace_unique_key_args(_Key const& __k, _Args&&... __args);
Howard Hinnant3e519522010-05-11 19:42:16 +00001020
Eric Fiselierfcd02212016-02-11 11:59:44 +00001021 template <class... _Args>
Duncan P. N. Exon Smithfde79b42016-03-17 20:45:20 +00001022 _LIBCPP_INLINE_VISIBILITY
1023 pair<iterator, bool> __emplace_unique_impl(_Args&&... __args);
1024
1025 template <class _Pp>
1026 _LIBCPP_INLINE_VISIBILITY
1027 pair<iterator, bool> __emplace_unique(_Pp&& __x) {
1028 return __emplace_unique_extract_key(_VSTD::forward<_Pp>(__x),
1029 __can_extract_key<_Pp, key_type>());
1030 }
Eric Fiselier50088682016-04-16 00:23:12 +00001031
1032 template <class _First, class _Second>
1033 _LIBCPP_INLINE_VISIBILITY
1034 typename enable_if<
1035 __can_extract_map_key<_First, key_type, __container_value_type>::value,
1036 pair<iterator, bool>
1037 >::type __emplace_unique(_First&& __f, _Second&& __s) {
1038 return __emplace_unique_key_args(__f, _VSTD::forward<_First>(__f),
1039 _VSTD::forward<_Second>(__s));
1040 }
1041
Eric Fiselierfcd02212016-02-11 11:59:44 +00001042 template <class... _Args>
Duncan P. N. Exon Smithfde79b42016-03-17 20:45:20 +00001043 _LIBCPP_INLINE_VISIBILITY
1044 pair<iterator, bool> __emplace_unique(_Args&&... __args) {
1045 return __emplace_unique_impl(_VSTD::forward<_Args>(__args)...);
1046 }
1047
1048 template <class _Pp>
1049 _LIBCPP_INLINE_VISIBILITY
1050 pair<iterator, bool>
1051 __emplace_unique_extract_key(_Pp&& __x, __extract_key_fail_tag) {
1052 return __emplace_unique_impl(_VSTD::forward<_Pp>(__x));
1053 }
1054 template <class _Pp>
1055 _LIBCPP_INLINE_VISIBILITY
1056 pair<iterator, bool>
1057 __emplace_unique_extract_key(_Pp&& __x, __extract_key_self_tag) {
1058 return __emplace_unique_key_args(__x, _VSTD::forward<_Pp>(__x));
1059 }
1060 template <class _Pp>
1061 _LIBCPP_INLINE_VISIBILITY
1062 pair<iterator, bool>
1063 __emplace_unique_extract_key(_Pp&& __x, __extract_key_first_tag) {
1064 return __emplace_unique_key_args(__x.first, _VSTD::forward<_Pp>(__x));
1065 }
1066
1067 template <class... _Args>
1068 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierfcd02212016-02-11 11:59:44 +00001069 iterator __emplace_multi(_Args&&... __args);
1070 template <class... _Args>
Duncan P. N. Exon Smithfde79b42016-03-17 20:45:20 +00001071 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierfcd02212016-02-11 11:59:44 +00001072 iterator __emplace_hint_multi(const_iterator __p, _Args&&... __args);
1073
1074
Eric Fiselierde3f2b32015-06-13 07:18:32 +00001075 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierfcd02212016-02-11 11:59:44 +00001076 pair<iterator, bool>
1077 __insert_unique(__container_value_type&& __x) {
1078 return __emplace_unique_key_args(_NodeTypes::__get_key(__x), _VSTD::move(__x));
1079 }
1080
1081 template <class _Pp, class = typename enable_if<
1082 !__is_same_uncvref<_Pp, __container_value_type>::value
1083 >::type>
Eric Fiselierde3f2b32015-06-13 07:18:32 +00001084 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierfcd02212016-02-11 11:59:44 +00001085 pair<iterator, bool> __insert_unique(_Pp&& __x) {
1086 return __emplace_unique(_VSTD::forward<_Pp>(__x));
1087 }
1088
1089 template <class _Pp>
1090 _LIBCPP_INLINE_VISIBILITY
1091 iterator __insert_multi(_Pp&& __x) {
1092 return __emplace_multi(_VSTD::forward<_Pp>(__x));
1093 }
1094
1095 template <class _Pp>
1096 _LIBCPP_INLINE_VISIBILITY
1097 iterator __insert_multi(const_iterator __p, _Pp&& __x) {
1098 return __emplace_hint_multi(__p, _VSTD::forward<_Pp>(__x));
1099 }
1100
1101#else // !defined(_LIBCPP_CXX03_LANG)
1102 template <class _Key, class _Args>
Eric Fiselier4271d012016-09-25 04:05:46 +00001103 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierfcd02212016-02-11 11:59:44 +00001104 pair<iterator, bool> __emplace_unique_key_args(_Key const&, _Args& __args);
1105
1106 iterator __insert_multi(const __container_value_type& __x);
1107 iterator __insert_multi(const_iterator __p, const __container_value_type& __x);
Eric Fiselierde3f2b32015-06-13 07:18:32 +00001108#endif
1109
Eric Fiselierfcd02212016-02-11 11:59:44 +00001110 _LIBCPP_INLINE_VISIBILITY
1111 pair<iterator, bool> __insert_unique(const __container_value_type& __x) {
1112 return __emplace_unique_key_args(_NodeTypes::__get_key(__x), __x);
1113 }
Howard Hinnant3e519522010-05-11 19:42:16 +00001114
Howard Hinnant37141072011-06-04 18:54:24 +00001115 void clear() _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +00001116 void rehash(size_type __n);
Howard Hinnant43d99232010-09-21 17:32:39 +00001117 _LIBCPP_INLINE_VISIBILITY void reserve(size_type __n)
Howard Hinnant3e519522010-05-11 19:42:16 +00001118 {rehash(static_cast<size_type>(ceil(__n / max_load_factor())));}
Howard Hinnant43d99232010-09-21 17:32:39 +00001119
1120 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001121 size_type bucket_count() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001122 {
1123 return __bucket_list_.get_deleter().size();
1124 }
1125
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001126 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001127 iterator begin() _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001128 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001129 iterator end() _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001130 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001131 const_iterator begin() const _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001132 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001133 const_iterator end() const _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +00001134
1135 template <class _Key>
Howard Hinnant43d99232010-09-21 17:32:39 +00001136 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001137 size_type bucket(const _Key& __k) const
Howard Hinnante5c13de2013-07-29 19:05:47 +00001138 {
1139 _LIBCPP_ASSERT(bucket_count() > 0,
1140 "unordered container::bucket(key) called when bucket_count() == 0");
1141 return __constrain_hash(hash_function()(__k), bucket_count());
1142 }
Howard Hinnant3e519522010-05-11 19:42:16 +00001143
1144 template <class _Key>
1145 iterator find(const _Key& __x);
1146 template <class _Key>
1147 const_iterator find(const _Key& __x) const;
1148
Howard Hinnantc003db12011-11-29 18:15:50 +00001149 typedef __hash_node_destructor<__node_allocator> _Dp;
1150 typedef unique_ptr<__node, _Dp> __node_holder;
Howard Hinnant3e519522010-05-11 19:42:16 +00001151
1152 iterator erase(const_iterator __p);
1153 iterator erase(const_iterator __first, const_iterator __last);
1154 template <class _Key>
1155 size_type __erase_unique(const _Key& __k);
1156 template <class _Key>
1157 size_type __erase_multi(const _Key& __k);
Howard Hinnant37141072011-06-04 18:54:24 +00001158 __node_holder remove(const_iterator __p) _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +00001159
1160 template <class _Key>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001161 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001162 size_type __count_unique(const _Key& __k) const;
1163 template <class _Key>
1164 size_type __count_multi(const _Key& __k) const;
1165
1166 template <class _Key>
1167 pair<iterator, iterator>
1168 __equal_range_unique(const _Key& __k);
1169 template <class _Key>
1170 pair<const_iterator, const_iterator>
1171 __equal_range_unique(const _Key& __k) const;
1172
1173 template <class _Key>
1174 pair<iterator, iterator>
1175 __equal_range_multi(const _Key& __k);
1176 template <class _Key>
1177 pair<const_iterator, const_iterator>
1178 __equal_range_multi(const _Key& __k) const;
1179
Howard Hinnant37141072011-06-04 18:54:24 +00001180 void swap(__hash_table& __u)
Eric Fiselier87a82492015-07-18 20:40:46 +00001181#if _LIBCPP_STD_VER <= 11
Howard Hinnant37141072011-06-04 18:54:24 +00001182 _NOEXCEPT_(
Marshall Clowe3fbe142015-07-13 20:04:56 +00001183 __is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value
Marshall Clowe3fbe142015-07-13 20:04:56 +00001184 && (!allocator_traits<__pointer_allocator>::propagate_on_container_swap::value
1185 || __is_nothrow_swappable<__pointer_allocator>::value)
1186 && (!__node_traits::propagate_on_container_swap::value
1187 || __is_nothrow_swappable<__node_allocator>::value)
Marshall Clowe3fbe142015-07-13 20:04:56 +00001188 );
Eric Fiselier87a82492015-07-18 20:40:46 +00001189#else
1190 _NOEXCEPT_(__is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value);
1191#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001192
Howard Hinnant43d99232010-09-21 17:32:39 +00001193 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001194 size_type max_bucket_count() const _NOEXCEPT
Howard Hinnant307f8142013-06-22 15:21:29 +00001195 {return __pointer_alloc_traits::max_size(__bucket_list_.get_deleter().__alloc());}
Howard Hinnant3e519522010-05-11 19:42:16 +00001196 size_type bucket_size(size_type __n) const;
Howard Hinnant37141072011-06-04 18:54:24 +00001197 _LIBCPP_INLINE_VISIBILITY float load_factor() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001198 {
1199 size_type __bc = bucket_count();
1200 return __bc != 0 ? (float)size() / __bc : 0.f;
1201 }
Howard Hinnant37141072011-06-04 18:54:24 +00001202 _LIBCPP_INLINE_VISIBILITY void max_load_factor(float __mlf) _NOEXCEPT
Howard Hinnante5c13de2013-07-29 19:05:47 +00001203 {
1204 _LIBCPP_ASSERT(__mlf > 0,
1205 "unordered container::max_load_factor(lf) called with lf <= 0");
1206 max_load_factor() = _VSTD::max(__mlf, load_factor());
1207 }
Howard Hinnant3e519522010-05-11 19:42:16 +00001208
Howard Hinnantb24c8022013-07-23 22:01:58 +00001209 _LIBCPP_INLINE_VISIBILITY
1210 local_iterator
1211 begin(size_type __n)
1212 {
Howard Hinnante5c13de2013-07-29 19:05:47 +00001213 _LIBCPP_ASSERT(__n < bucket_count(),
1214 "unordered container::begin(n) called with n >= bucket_count()");
Howard Hinnantb24c8022013-07-23 22:01:58 +00001215#if _LIBCPP_DEBUG_LEVEL >= 2
1216 return local_iterator(__bucket_list_[__n], __n, bucket_count(), this);
1217#else
1218 return local_iterator(__bucket_list_[__n], __n, bucket_count());
1219#endif
1220 }
1221
1222 _LIBCPP_INLINE_VISIBILITY
1223 local_iterator
1224 end(size_type __n)
1225 {
Howard Hinnante5c13de2013-07-29 19:05:47 +00001226 _LIBCPP_ASSERT(__n < bucket_count(),
1227 "unordered container::end(n) called with n >= bucket_count()");
Howard Hinnantb24c8022013-07-23 22:01:58 +00001228#if _LIBCPP_DEBUG_LEVEL >= 2
1229 return local_iterator(nullptr, __n, bucket_count(), this);
1230#else
1231 return local_iterator(nullptr, __n, bucket_count());
1232#endif
1233 }
1234
1235 _LIBCPP_INLINE_VISIBILITY
1236 const_local_iterator
1237 cbegin(size_type __n) const
1238 {
Howard Hinnante5c13de2013-07-29 19:05:47 +00001239 _LIBCPP_ASSERT(__n < bucket_count(),
1240 "unordered container::cbegin(n) called with n >= bucket_count()");
Howard Hinnantb24c8022013-07-23 22:01:58 +00001241#if _LIBCPP_DEBUG_LEVEL >= 2
1242 return const_local_iterator(__bucket_list_[__n], __n, bucket_count(), this);
1243#else
1244 return const_local_iterator(__bucket_list_[__n], __n, bucket_count());
1245#endif
1246 }
1247
1248 _LIBCPP_INLINE_VISIBILITY
1249 const_local_iterator
1250 cend(size_type __n) const
1251 {
Howard Hinnante5c13de2013-07-29 19:05:47 +00001252 _LIBCPP_ASSERT(__n < bucket_count(),
1253 "unordered container::cend(n) called with n >= bucket_count()");
Howard Hinnantb24c8022013-07-23 22:01:58 +00001254#if _LIBCPP_DEBUG_LEVEL >= 2
1255 return const_local_iterator(nullptr, __n, bucket_count(), this);
1256#else
1257 return const_local_iterator(nullptr, __n, bucket_count());
1258#endif
1259 }
1260
1261#if _LIBCPP_DEBUG_LEVEL >= 2
1262
1263 bool __dereferenceable(const const_iterator* __i) const;
1264 bool __decrementable(const const_iterator* __i) const;
1265 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1266 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1267
1268#endif // _LIBCPP_DEBUG_LEVEL >= 2
1269
Howard Hinnant3e519522010-05-11 19:42:16 +00001270private:
1271 void __rehash(size_type __n);
1272
Eric Fiselierfcd02212016-02-11 11:59:44 +00001273#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001274 template <class ..._Args>
Eric Fiselierfcd02212016-02-11 11:59:44 +00001275 __node_holder __construct_node(_Args&& ...__args);
1276
1277 template <class _First, class ..._Rest>
1278 __node_holder __construct_node_hash(size_t __hash, _First&& __f, _Rest&&... __rest);
1279#else // _LIBCPP_CXX03_LANG
1280 __node_holder __construct_node(const __container_value_type& __v);
1281 __node_holder __construct_node_hash(size_t __hash, const __container_value_type& __v);
Howard Hinnant3e519522010-05-11 19:42:16 +00001282#endif
Eric Fiselierfcd02212016-02-11 11:59:44 +00001283
Howard Hinnant3e519522010-05-11 19:42:16 +00001284
Howard Hinnant43d99232010-09-21 17:32:39 +00001285 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001286 void __copy_assign_alloc(const __hash_table& __u)
1287 {__copy_assign_alloc(__u, integral_constant<bool,
1288 __node_traits::propagate_on_container_copy_assignment::value>());}
1289 void __copy_assign_alloc(const __hash_table& __u, true_type);
Howard Hinnant43d99232010-09-21 17:32:39 +00001290 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2063662011-12-01 20:21:04 +00001291 void __copy_assign_alloc(const __hash_table&, false_type) {}
Howard Hinnant3e519522010-05-11 19:42:16 +00001292
Eric Fiselierfcd02212016-02-11 11:59:44 +00001293#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001294 void __move_assign(__hash_table& __u, false_type);
Howard Hinnant37141072011-06-04 18:54:24 +00001295 void __move_assign(__hash_table& __u, true_type)
1296 _NOEXCEPT_(
1297 is_nothrow_move_assignable<__node_allocator>::value &&
1298 is_nothrow_move_assignable<hasher>::value &&
1299 is_nothrow_move_assignable<key_equal>::value);
1300 _LIBCPP_INLINE_VISIBILITY
1301 void __move_assign_alloc(__hash_table& __u)
1302 _NOEXCEPT_(
1303 !__node_traits::propagate_on_container_move_assignment::value ||
1304 (is_nothrow_move_assignable<__pointer_allocator>::value &&
1305 is_nothrow_move_assignable<__node_allocator>::value))
Howard Hinnant3e519522010-05-11 19:42:16 +00001306 {__move_assign_alloc(__u, integral_constant<bool,
1307 __node_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnant43d99232010-09-21 17:32:39 +00001308 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001309 void __move_assign_alloc(__hash_table& __u, true_type)
Howard Hinnant37141072011-06-04 18:54:24 +00001310 _NOEXCEPT_(
1311 is_nothrow_move_assignable<__pointer_allocator>::value &&
1312 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +00001313 {
1314 __bucket_list_.get_deleter().__alloc() =
Howard Hinnantce48a112011-06-30 21:18:19 +00001315 _VSTD::move(__u.__bucket_list_.get_deleter().__alloc());
1316 __node_alloc() = _VSTD::move(__u.__node_alloc());
Howard Hinnant3e519522010-05-11 19:42:16 +00001317 }
Howard Hinnant43d99232010-09-21 17:32:39 +00001318 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001319 void __move_assign_alloc(__hash_table&, false_type) _NOEXCEPT {}
Eric Fiselierfcd02212016-02-11 11:59:44 +00001320#endif // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001321
Eric Fiselier40492ba2016-07-23 20:36:55 +00001322 void __deallocate(__next_pointer __np) _NOEXCEPT;
1323 __next_pointer __detach() _NOEXCEPT;
Howard Hinnant307f8142013-06-22 15:21:29 +00001324
Howard Hinnantf0544c22013-08-12 18:38:34 +00001325 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_map;
1326 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_multimap;
Howard Hinnant3e519522010-05-11 19:42:16 +00001327};
1328
1329template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001330inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001331__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table()
Howard Hinnant37141072011-06-04 18:54:24 +00001332 _NOEXCEPT_(
1333 is_nothrow_default_constructible<__bucket_list>::value &&
1334 is_nothrow_default_constructible<__first_node>::value &&
Eric Fiseliere2e332a2015-12-16 00:53:04 +00001335 is_nothrow_default_constructible<__node_allocator>::value &&
Howard Hinnant37141072011-06-04 18:54:24 +00001336 is_nothrow_default_constructible<hasher>::value &&
1337 is_nothrow_default_constructible<key_equal>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +00001338 : __p2_(0),
1339 __p3_(1.0f)
1340{
1341}
1342
1343template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001344inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001345__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const hasher& __hf,
1346 const key_equal& __eql)
1347 : __bucket_list_(nullptr, __bucket_list_deleter()),
1348 __p1_(),
1349 __p2_(0, __hf),
1350 __p3_(1.0f, __eql)
1351{
1352}
1353
1354template <class _Tp, class _Hash, class _Equal, class _Alloc>
1355__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const hasher& __hf,
1356 const key_equal& __eql,
1357 const allocator_type& __a)
1358 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1359 __p1_(__node_allocator(__a)),
1360 __p2_(0, __hf),
1361 __p3_(1.0f, __eql)
1362{
1363}
1364
1365template <class _Tp, class _Hash, class _Equal, class _Alloc>
1366__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const allocator_type& __a)
1367 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1368 __p1_(__node_allocator(__a)),
1369 __p2_(0),
1370 __p3_(1.0f)
1371{
1372}
1373
1374template <class _Tp, class _Hash, class _Equal, class _Alloc>
1375__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const __hash_table& __u)
1376 : __bucket_list_(nullptr,
1377 __bucket_list_deleter(allocator_traits<__pointer_allocator>::
1378 select_on_container_copy_construction(
1379 __u.__bucket_list_.get_deleter().__alloc()), 0)),
1380 __p1_(allocator_traits<__node_allocator>::
1381 select_on_container_copy_construction(__u.__node_alloc())),
1382 __p2_(0, __u.hash_function()),
1383 __p3_(__u.__p3_)
1384{
1385}
1386
1387template <class _Tp, class _Hash, class _Equal, class _Alloc>
1388__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const __hash_table& __u,
1389 const allocator_type& __a)
1390 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1391 __p1_(__node_allocator(__a)),
1392 __p2_(0, __u.hash_function()),
1393 __p3_(__u.__p3_)
1394{
1395}
1396
Eric Fiselierfcd02212016-02-11 11:59:44 +00001397#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001398
1399template <class _Tp, class _Hash, class _Equal, class _Alloc>
1400__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(__hash_table&& __u)
Howard Hinnant37141072011-06-04 18:54:24 +00001401 _NOEXCEPT_(
1402 is_nothrow_move_constructible<__bucket_list>::value &&
1403 is_nothrow_move_constructible<__first_node>::value &&
Eric Fiseliere2e332a2015-12-16 00:53:04 +00001404 is_nothrow_move_constructible<__node_allocator>::value &&
Howard Hinnant37141072011-06-04 18:54:24 +00001405 is_nothrow_move_constructible<hasher>::value &&
1406 is_nothrow_move_constructible<key_equal>::value)
Howard Hinnantce48a112011-06-30 21:18:19 +00001407 : __bucket_list_(_VSTD::move(__u.__bucket_list_)),
1408 __p1_(_VSTD::move(__u.__p1_)),
1409 __p2_(_VSTD::move(__u.__p2_)),
1410 __p3_(_VSTD::move(__u.__p3_))
Howard Hinnant3e519522010-05-11 19:42:16 +00001411{
1412 if (size() > 0)
1413 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001414 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash(), bucket_count())] =
1415 __p1_.first().__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001416 __u.__p1_.first().__next_ = nullptr;
1417 __u.size() = 0;
1418 }
1419}
1420
1421template <class _Tp, class _Hash, class _Equal, class _Alloc>
1422__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(__hash_table&& __u,
1423 const allocator_type& __a)
1424 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1425 __p1_(__node_allocator(__a)),
Howard Hinnantce48a112011-06-30 21:18:19 +00001426 __p2_(0, _VSTD::move(__u.hash_function())),
1427 __p3_(_VSTD::move(__u.__p3_))
Howard Hinnant3e519522010-05-11 19:42:16 +00001428{
1429 if (__a == allocator_type(__u.__node_alloc()))
1430 {
1431 __bucket_list_.reset(__u.__bucket_list_.release());
1432 __bucket_list_.get_deleter().size() = __u.__bucket_list_.get_deleter().size();
1433 __u.__bucket_list_.get_deleter().size() = 0;
1434 if (__u.size() > 0)
1435 {
1436 __p1_.first().__next_ = __u.__p1_.first().__next_;
1437 __u.__p1_.first().__next_ = nullptr;
Eric Fiselier40492ba2016-07-23 20:36:55 +00001438 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash(), bucket_count())] =
1439 __p1_.first().__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001440 size() = __u.size();
1441 __u.size() = 0;
1442 }
1443 }
1444}
1445
Eric Fiselierfcd02212016-02-11 11:59:44 +00001446#endif // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001447
1448template <class _Tp, class _Hash, class _Equal, class _Alloc>
1449__hash_table<_Tp, _Hash, _Equal, _Alloc>::~__hash_table()
1450{
Marshall Clow3b8669e2016-06-30 22:05:45 +00001451 static_assert((is_copy_constructible<key_equal>::value),
1452 "Predicate must be copy-constructible.");
1453 static_assert((is_copy_constructible<hasher>::value),
1454 "Hasher must be copy-constructible.");
Howard Hinnant3e519522010-05-11 19:42:16 +00001455 __deallocate(__p1_.first().__next_);
Howard Hinnantb24c8022013-07-23 22:01:58 +00001456#if _LIBCPP_DEBUG_LEVEL >= 2
1457 __get_db()->__erase_c(this);
1458#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001459}
1460
1461template <class _Tp, class _Hash, class _Equal, class _Alloc>
1462void
1463__hash_table<_Tp, _Hash, _Equal, _Alloc>::__copy_assign_alloc(
1464 const __hash_table& __u, true_type)
1465{
1466 if (__node_alloc() != __u.__node_alloc())
1467 {
1468 clear();
1469 __bucket_list_.reset();
1470 __bucket_list_.get_deleter().size() = 0;
1471 }
1472 __bucket_list_.get_deleter().__alloc() = __u.__bucket_list_.get_deleter().__alloc();
1473 __node_alloc() = __u.__node_alloc();
1474}
1475
1476template <class _Tp, class _Hash, class _Equal, class _Alloc>
1477__hash_table<_Tp, _Hash, _Equal, _Alloc>&
1478__hash_table<_Tp, _Hash, _Equal, _Alloc>::operator=(const __hash_table& __u)
1479{
1480 if (this != &__u)
1481 {
1482 __copy_assign_alloc(__u);
1483 hash_function() = __u.hash_function();
1484 key_eq() = __u.key_eq();
1485 max_load_factor() = __u.max_load_factor();
1486 __assign_multi(__u.begin(), __u.end());
1487 }
1488 return *this;
1489}
1490
1491template <class _Tp, class _Hash, class _Equal, class _Alloc>
1492void
Eric Fiselier40492ba2016-07-23 20:36:55 +00001493__hash_table<_Tp, _Hash, _Equal, _Alloc>::__deallocate(__next_pointer __np)
Howard Hinnant37141072011-06-04 18:54:24 +00001494 _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001495{
1496 __node_allocator& __na = __node_alloc();
1497 while (__np != nullptr)
1498 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001499 __next_pointer __next = __np->__next_;
Howard Hinnantb24c8022013-07-23 22:01:58 +00001500#if _LIBCPP_DEBUG_LEVEL >= 2
1501 __c_node* __c = __get_db()->__find_c_and_lock(this);
1502 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1503 {
1504 --__p;
1505 iterator* __i = static_cast<iterator*>((*__p)->__i_);
1506 if (__i->__node_ == __np)
1507 {
1508 (*__p)->__c_ = nullptr;
1509 if (--__c->end_ != __p)
1510 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1511 }
1512 }
1513 __get_db()->unlock();
1514#endif
Eric Fiselier40492ba2016-07-23 20:36:55 +00001515 __node_pointer __real_np = __np->__upcast();
1516 __node_traits::destroy(__na, _NodeTypes::__get_ptr(__real_np->__value_));
1517 __node_traits::deallocate(__na, __real_np, 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001518 __np = __next;
1519 }
1520}
1521
1522template <class _Tp, class _Hash, class _Equal, class _Alloc>
Eric Fiselier40492ba2016-07-23 20:36:55 +00001523typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__next_pointer
Howard Hinnant37141072011-06-04 18:54:24 +00001524__hash_table<_Tp, _Hash, _Equal, _Alloc>::__detach() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001525{
1526 size_type __bc = bucket_count();
1527 for (size_type __i = 0; __i < __bc; ++__i)
1528 __bucket_list_[__i] = nullptr;
1529 size() = 0;
Eric Fiselier40492ba2016-07-23 20:36:55 +00001530 __next_pointer __cache = __p1_.first().__next_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001531 __p1_.first().__next_ = nullptr;
1532 return __cache;
1533}
1534
Eric Fiselierfcd02212016-02-11 11:59:44 +00001535#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001536
1537template <class _Tp, class _Hash, class _Equal, class _Alloc>
1538void
1539__hash_table<_Tp, _Hash, _Equal, _Alloc>::__move_assign(
1540 __hash_table& __u, true_type)
Howard Hinnant37141072011-06-04 18:54:24 +00001541 _NOEXCEPT_(
1542 is_nothrow_move_assignable<__node_allocator>::value &&
1543 is_nothrow_move_assignable<hasher>::value &&
1544 is_nothrow_move_assignable<key_equal>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +00001545{
1546 clear();
1547 __bucket_list_.reset(__u.__bucket_list_.release());
1548 __bucket_list_.get_deleter().size() = __u.__bucket_list_.get_deleter().size();
1549 __u.__bucket_list_.get_deleter().size() = 0;
1550 __move_assign_alloc(__u);
1551 size() = __u.size();
Howard Hinnantce48a112011-06-30 21:18:19 +00001552 hash_function() = _VSTD::move(__u.hash_function());
Howard Hinnant3e519522010-05-11 19:42:16 +00001553 max_load_factor() = __u.max_load_factor();
Howard Hinnantce48a112011-06-30 21:18:19 +00001554 key_eq() = _VSTD::move(__u.key_eq());
Howard Hinnant3e519522010-05-11 19:42:16 +00001555 __p1_.first().__next_ = __u.__p1_.first().__next_;
1556 if (size() > 0)
1557 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001558 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash(), bucket_count())] =
1559 __p1_.first().__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001560 __u.__p1_.first().__next_ = nullptr;
1561 __u.size() = 0;
1562 }
Howard Hinnantb24c8022013-07-23 22:01:58 +00001563#if _LIBCPP_DEBUG_LEVEL >= 2
1564 __get_db()->swap(this, &__u);
1565#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001566}
1567
1568template <class _Tp, class _Hash, class _Equal, class _Alloc>
1569void
1570__hash_table<_Tp, _Hash, _Equal, _Alloc>::__move_assign(
1571 __hash_table& __u, false_type)
1572{
1573 if (__node_alloc() == __u.__node_alloc())
1574 __move_assign(__u, true_type());
1575 else
1576 {
Howard Hinnantce48a112011-06-30 21:18:19 +00001577 hash_function() = _VSTD::move(__u.hash_function());
1578 key_eq() = _VSTD::move(__u.key_eq());
Howard Hinnant3e519522010-05-11 19:42:16 +00001579 max_load_factor() = __u.max_load_factor();
1580 if (bucket_count() != 0)
1581 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001582 __next_pointer __cache = __detach();
Howard Hinnant3e519522010-05-11 19:42:16 +00001583#ifndef _LIBCPP_NO_EXCEPTIONS
1584 try
1585 {
Howard Hinnantb3371f62010-08-22 00:02:43 +00001586#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001587 const_iterator __i = __u.begin();
1588 while (__cache != nullptr && __u.size() != 0)
1589 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001590 __cache->__upcast()->__value_ =
1591 _VSTD::move(__u.remove(__i++)->__value_);
1592 __next_pointer __next = __cache->__next_;
1593 __node_insert_multi(__cache->__upcast());
Howard Hinnant3e519522010-05-11 19:42:16 +00001594 __cache = __next;
1595 }
1596#ifndef _LIBCPP_NO_EXCEPTIONS
1597 }
1598 catch (...)
1599 {
1600 __deallocate(__cache);
1601 throw;
1602 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00001603#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001604 __deallocate(__cache);
1605 }
1606 const_iterator __i = __u.begin();
1607 while (__u.size() != 0)
1608 {
Eric Fiselierfcd02212016-02-11 11:59:44 +00001609 __node_holder __h = __construct_node(_NodeTypes::__move(__u.remove(__i++)->__value_));
Howard Hinnant3e519522010-05-11 19:42:16 +00001610 __node_insert_multi(__h.get());
1611 __h.release();
1612 }
1613 }
1614}
1615
1616template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001617inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001618__hash_table<_Tp, _Hash, _Equal, _Alloc>&
1619__hash_table<_Tp, _Hash, _Equal, _Alloc>::operator=(__hash_table&& __u)
Howard Hinnant37141072011-06-04 18:54:24 +00001620 _NOEXCEPT_(
1621 __node_traits::propagate_on_container_move_assignment::value &&
1622 is_nothrow_move_assignable<__node_allocator>::value &&
1623 is_nothrow_move_assignable<hasher>::value &&
1624 is_nothrow_move_assignable<key_equal>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +00001625{
1626 __move_assign(__u, integral_constant<bool,
1627 __node_traits::propagate_on_container_move_assignment::value>());
1628 return *this;
1629}
1630
Eric Fiselierfcd02212016-02-11 11:59:44 +00001631#endif // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001632
1633template <class _Tp, class _Hash, class _Equal, class _Alloc>
1634template <class _InputIterator>
1635void
1636__hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_unique(_InputIterator __first,
1637 _InputIterator __last)
1638{
Eric Fiselierfcd02212016-02-11 11:59:44 +00001639 typedef iterator_traits<_InputIterator> _ITraits;
1640 typedef typename _ITraits::value_type _ItValueType;
1641 static_assert((is_same<_ItValueType, __container_value_type>::value),
1642 "__assign_unique may only be called with the containers value type");
1643
Howard Hinnant3e519522010-05-11 19:42:16 +00001644 if (bucket_count() != 0)
1645 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001646 __next_pointer __cache = __detach();
Howard Hinnant3e519522010-05-11 19:42:16 +00001647#ifndef _LIBCPP_NO_EXCEPTIONS
1648 try
1649 {
Howard Hinnantb3371f62010-08-22 00:02:43 +00001650#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001651 for (; __cache != nullptr && __first != __last; ++__first)
1652 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001653 __cache->__upcast()->__value_ = *__first;
1654 __next_pointer __next = __cache->__next_;
1655 __node_insert_unique(__cache->__upcast());
Howard Hinnant3e519522010-05-11 19:42:16 +00001656 __cache = __next;
1657 }
1658#ifndef _LIBCPP_NO_EXCEPTIONS
1659 }
1660 catch (...)
1661 {
1662 __deallocate(__cache);
1663 throw;
1664 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00001665#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001666 __deallocate(__cache);
1667 }
1668 for (; __first != __last; ++__first)
1669 __insert_unique(*__first);
1670}
1671
1672template <class _Tp, class _Hash, class _Equal, class _Alloc>
1673template <class _InputIterator>
1674void
1675__hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_multi(_InputIterator __first,
1676 _InputIterator __last)
1677{
Eric Fiselierfcd02212016-02-11 11:59:44 +00001678 typedef iterator_traits<_InputIterator> _ITraits;
1679 typedef typename _ITraits::value_type _ItValueType;
1680 static_assert((is_same<_ItValueType, __container_value_type>::value ||
1681 is_same<_ItValueType, __node_value_type>::value),
1682 "__assign_multi may only be called with the containers value type"
1683 " or the nodes value type");
Howard Hinnant3e519522010-05-11 19:42:16 +00001684 if (bucket_count() != 0)
1685 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001686 __next_pointer __cache = __detach();
Howard Hinnant3e519522010-05-11 19:42:16 +00001687#ifndef _LIBCPP_NO_EXCEPTIONS
1688 try
1689 {
Howard Hinnantb3371f62010-08-22 00:02:43 +00001690#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001691 for (; __cache != nullptr && __first != __last; ++__first)
1692 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001693 __cache->__upcast()->__value_ = *__first;
1694 __next_pointer __next = __cache->__next_;
1695 __node_insert_multi(__cache->__upcast());
Howard Hinnant3e519522010-05-11 19:42:16 +00001696 __cache = __next;
1697 }
1698#ifndef _LIBCPP_NO_EXCEPTIONS
1699 }
1700 catch (...)
1701 {
1702 __deallocate(__cache);
1703 throw;
1704 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00001705#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001706 __deallocate(__cache);
1707 }
1708 for (; __first != __last; ++__first)
Eric Fiselierfcd02212016-02-11 11:59:44 +00001709 __insert_multi(_NodeTypes::__get_value(*__first));
Howard Hinnant3e519522010-05-11 19:42:16 +00001710}
1711
1712template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001713inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001714typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
Howard Hinnant37141072011-06-04 18:54:24 +00001715__hash_table<_Tp, _Hash, _Equal, _Alloc>::begin() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001716{
Howard Hinnantb24c8022013-07-23 22:01:58 +00001717#if _LIBCPP_DEBUG_LEVEL >= 2
1718 return iterator(__p1_.first().__next_, this);
1719#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001720 return iterator(__p1_.first().__next_);
Howard Hinnantb24c8022013-07-23 22:01:58 +00001721#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001722}
1723
1724template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001725inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001726typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
Howard Hinnant37141072011-06-04 18:54:24 +00001727__hash_table<_Tp, _Hash, _Equal, _Alloc>::end() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001728{
Howard Hinnantb24c8022013-07-23 22:01:58 +00001729#if _LIBCPP_DEBUG_LEVEL >= 2
1730 return iterator(nullptr, this);
1731#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001732 return iterator(nullptr);
Howard Hinnantb24c8022013-07-23 22:01:58 +00001733#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001734}
1735
1736template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001737inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001738typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
Howard Hinnant37141072011-06-04 18:54:24 +00001739__hash_table<_Tp, _Hash, _Equal, _Alloc>::begin() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001740{
Howard Hinnantb24c8022013-07-23 22:01:58 +00001741#if _LIBCPP_DEBUG_LEVEL >= 2
1742 return const_iterator(__p1_.first().__next_, this);
1743#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001744 return const_iterator(__p1_.first().__next_);
Howard Hinnantb24c8022013-07-23 22:01:58 +00001745#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001746}
1747
1748template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001749inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001750typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
Howard Hinnant37141072011-06-04 18:54:24 +00001751__hash_table<_Tp, _Hash, _Equal, _Alloc>::end() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001752{
Howard Hinnantb24c8022013-07-23 22:01:58 +00001753#if _LIBCPP_DEBUG_LEVEL >= 2
1754 return const_iterator(nullptr, this);
1755#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001756 return const_iterator(nullptr);
Howard Hinnantb24c8022013-07-23 22:01:58 +00001757#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001758}
1759
1760template <class _Tp, class _Hash, class _Equal, class _Alloc>
1761void
Howard Hinnant37141072011-06-04 18:54:24 +00001762__hash_table<_Tp, _Hash, _Equal, _Alloc>::clear() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001763{
1764 if (size() > 0)
1765 {
1766 __deallocate(__p1_.first().__next_);
1767 __p1_.first().__next_ = nullptr;
1768 size_type __bc = bucket_count();
Howard Hinnant1f8da842011-07-05 14:14:17 +00001769 for (size_type __i = 0; __i < __bc; ++__i)
Howard Hinnant3e519522010-05-11 19:42:16 +00001770 __bucket_list_[__i] = nullptr;
1771 size() = 0;
1772 }
1773}
1774
1775template <class _Tp, class _Hash, class _Equal, class _Alloc>
1776pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
1777__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_unique(__node_pointer __nd)
1778{
1779 __nd->__hash_ = hash_function()(__nd->__value_);
1780 size_type __bc = bucket_count();
1781 bool __inserted = false;
Eric Fiselier40492ba2016-07-23 20:36:55 +00001782 __next_pointer __ndptr;
Howard Hinnant3e519522010-05-11 19:42:16 +00001783 size_t __chash;
1784 if (__bc != 0)
1785 {
Howard Hinnant4cb38a82012-07-06 17:31:14 +00001786 __chash = __constrain_hash(__nd->__hash_, __bc);
Howard Hinnant3e519522010-05-11 19:42:16 +00001787 __ndptr = __bucket_list_[__chash];
1788 if (__ndptr != nullptr)
1789 {
1790 for (__ndptr = __ndptr->__next_; __ndptr != nullptr &&
Eric Fiselier40492ba2016-07-23 20:36:55 +00001791 __constrain_hash(__ndptr->__hash(), __bc) == __chash;
Howard Hinnant3e519522010-05-11 19:42:16 +00001792 __ndptr = __ndptr->__next_)
1793 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001794 if (key_eq()(__ndptr->__upcast()->__value_, __nd->__value_))
Howard Hinnant3e519522010-05-11 19:42:16 +00001795 goto __done;
1796 }
1797 }
1798 }
1799 {
1800 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1801 {
Eric Fiselier9ba5c112015-02-02 21:31:48 +00001802 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnant3e519522010-05-11 19:42:16 +00001803 size_type(ceil(float(size() + 1) / max_load_factor()))));
1804 __bc = bucket_count();
Howard Hinnant4cb38a82012-07-06 17:31:14 +00001805 __chash = __constrain_hash(__nd->__hash_, __bc);
Howard Hinnant3e519522010-05-11 19:42:16 +00001806 }
1807 // insert_after __bucket_list_[__chash], or __first_node if bucket is null
Eric Fiselier40492ba2016-07-23 20:36:55 +00001808 __next_pointer __pn = __bucket_list_[__chash];
Howard Hinnant3e519522010-05-11 19:42:16 +00001809 if (__pn == nullptr)
1810 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001811 __pn =__p1_.first().__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001812 __nd->__next_ = __pn->__next_;
Eric Fiselier40492ba2016-07-23 20:36:55 +00001813 __pn->__next_ = __nd->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001814 // fix up __bucket_list_
1815 __bucket_list_[__chash] = __pn;
1816 if (__nd->__next_ != nullptr)
Eric Fiselier40492ba2016-07-23 20:36:55 +00001817 __bucket_list_[__constrain_hash(__nd->__next_->__hash(), __bc)] = __nd->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001818 }
1819 else
1820 {
1821 __nd->__next_ = __pn->__next_;
Eric Fiselier40492ba2016-07-23 20:36:55 +00001822 __pn->__next_ = __nd->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001823 }
Eric Fiselier40492ba2016-07-23 20:36:55 +00001824 __ndptr = __nd->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001825 // increment size
1826 ++size();
1827 __inserted = true;
1828 }
1829__done:
Howard Hinnantb24c8022013-07-23 22:01:58 +00001830#if _LIBCPP_DEBUG_LEVEL >= 2
1831 return pair<iterator, bool>(iterator(__ndptr, this), __inserted);
1832#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001833 return pair<iterator, bool>(iterator(__ndptr), __inserted);
Howard Hinnantb24c8022013-07-23 22:01:58 +00001834#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001835}
1836
1837template <class _Tp, class _Hash, class _Equal, class _Alloc>
1838typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1839__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi(__node_pointer __cp)
1840{
1841 __cp->__hash_ = hash_function()(__cp->__value_);
1842 size_type __bc = bucket_count();
1843 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1844 {
Eric Fiselier9ba5c112015-02-02 21:31:48 +00001845 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnant3e519522010-05-11 19:42:16 +00001846 size_type(ceil(float(size() + 1) / max_load_factor()))));
1847 __bc = bucket_count();
1848 }
Howard Hinnant4cb38a82012-07-06 17:31:14 +00001849 size_t __chash = __constrain_hash(__cp->__hash_, __bc);
Eric Fiselier40492ba2016-07-23 20:36:55 +00001850 __next_pointer __pn = __bucket_list_[__chash];
Howard Hinnant3e519522010-05-11 19:42:16 +00001851 if (__pn == nullptr)
1852 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001853 __pn =__p1_.first().__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001854 __cp->__next_ = __pn->__next_;
Eric Fiselier40492ba2016-07-23 20:36:55 +00001855 __pn->__next_ = __cp->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001856 // fix up __bucket_list_
1857 __bucket_list_[__chash] = __pn;
1858 if (__cp->__next_ != nullptr)
Eric Fiselier40492ba2016-07-23 20:36:55 +00001859 __bucket_list_[__constrain_hash(__cp->__next_->__hash(), __bc)]
1860 = __cp->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001861 }
1862 else
1863 {
1864 for (bool __found = false; __pn->__next_ != nullptr &&
Eric Fiselier40492ba2016-07-23 20:36:55 +00001865 __constrain_hash(__pn->__next_->__hash(), __bc) == __chash;
Howard Hinnant3e519522010-05-11 19:42:16 +00001866 __pn = __pn->__next_)
Howard Hinnantb3371f62010-08-22 00:02:43 +00001867 {
Howard Hinnant3e519522010-05-11 19:42:16 +00001868 // __found key_eq() action
1869 // false false loop
1870 // true true loop
1871 // false true set __found to true
1872 // true false break
Eric Fiselier40492ba2016-07-23 20:36:55 +00001873 if (__found != (__pn->__next_->__hash() == __cp->__hash_ &&
1874 key_eq()(__pn->__next_->__upcast()->__value_, __cp->__value_)))
Howard Hinnant3e519522010-05-11 19:42:16 +00001875 {
1876 if (!__found)
1877 __found = true;
1878 else
1879 break;
1880 }
1881 }
1882 __cp->__next_ = __pn->__next_;
Eric Fiselier40492ba2016-07-23 20:36:55 +00001883 __pn->__next_ = __cp->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001884 if (__cp->__next_ != nullptr)
1885 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001886 size_t __nhash = __constrain_hash(__cp->__next_->__hash(), __bc);
Howard Hinnant3e519522010-05-11 19:42:16 +00001887 if (__nhash != __chash)
Eric Fiselier40492ba2016-07-23 20:36:55 +00001888 __bucket_list_[__nhash] = __cp->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001889 }
1890 }
1891 ++size();
Howard Hinnantb24c8022013-07-23 22:01:58 +00001892#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselier40492ba2016-07-23 20:36:55 +00001893 return iterator(__cp->__ptr(), this);
Howard Hinnantb24c8022013-07-23 22:01:58 +00001894#else
Eric Fiselier40492ba2016-07-23 20:36:55 +00001895 return iterator(__cp->__ptr());
Howard Hinnantb24c8022013-07-23 22:01:58 +00001896#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001897}
1898
1899template <class _Tp, class _Hash, class _Equal, class _Alloc>
1900typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1901__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi(
1902 const_iterator __p, __node_pointer __cp)
1903{
Howard Hinnant2f51de52013-08-02 17:50:49 +00001904#if _LIBCPP_DEBUG_LEVEL >= 2
1905 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1906 "unordered container::emplace_hint(const_iterator, args...) called with an iterator not"
1907 " referring to this unordered container");
1908#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001909 if (__p != end() && key_eq()(*__p, __cp->__value_))
1910 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001911 __next_pointer __np = __p.__node_;
1912 __cp->__hash_ = __np->__hash();
Howard Hinnant3e519522010-05-11 19:42:16 +00001913 size_type __bc = bucket_count();
1914 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1915 {
Eric Fiselier9ba5c112015-02-02 21:31:48 +00001916 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnant3e519522010-05-11 19:42:16 +00001917 size_type(ceil(float(size() + 1) / max_load_factor()))));
1918 __bc = bucket_count();
1919 }
Howard Hinnant4cb38a82012-07-06 17:31:14 +00001920 size_t __chash = __constrain_hash(__cp->__hash_, __bc);
Eric Fiselier40492ba2016-07-23 20:36:55 +00001921 __next_pointer __pp = __bucket_list_[__chash];
Howard Hinnant3e519522010-05-11 19:42:16 +00001922 while (__pp->__next_ != __np)
1923 __pp = __pp->__next_;
1924 __cp->__next_ = __np;
Eric Fiselier40492ba2016-07-23 20:36:55 +00001925 __pp->__next_ = static_cast<__next_pointer>(__cp);
Howard Hinnant3e519522010-05-11 19:42:16 +00001926 ++size();
Howard Hinnantb24c8022013-07-23 22:01:58 +00001927#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselier40492ba2016-07-23 20:36:55 +00001928 return iterator(static_cast<__next_pointer>(__cp), this);
Howard Hinnantb24c8022013-07-23 22:01:58 +00001929#else
Eric Fiselier40492ba2016-07-23 20:36:55 +00001930 return iterator(static_cast<__next_pointer>(__cp));
Howard Hinnantb24c8022013-07-23 22:01:58 +00001931#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001932 }
1933 return __node_insert_multi(__cp);
1934}
1935
Eric Fiselierde3f2b32015-06-13 07:18:32 +00001936
1937
Eric Fiselierfcd02212016-02-11 11:59:44 +00001938#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierde3f2b32015-06-13 07:18:32 +00001939template <class _Tp, class _Hash, class _Equal, class _Alloc>
Eric Fiselierfcd02212016-02-11 11:59:44 +00001940template <class _Key, class ..._Args>
Eric Fiselierde3f2b32015-06-13 07:18:32 +00001941pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
Eric Fiselierfcd02212016-02-11 11:59:44 +00001942__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_key_args(_Key const& __k, _Args&&... __args)
Eric Fiselierde3f2b32015-06-13 07:18:32 +00001943#else
1944template <class _Tp, class _Hash, class _Equal, class _Alloc>
Eric Fiselierfcd02212016-02-11 11:59:44 +00001945template <class _Key, class _Args>
Eric Fiselierde3f2b32015-06-13 07:18:32 +00001946pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
Eric Fiselierfcd02212016-02-11 11:59:44 +00001947__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_key_args(_Key const& __k, _Args& __args)
Eric Fiselierde3f2b32015-06-13 07:18:32 +00001948#endif
1949{
Eric Fiselierfcd02212016-02-11 11:59:44 +00001950
1951 size_t __hash = hash_function()(__k);
Howard Hinnant3e519522010-05-11 19:42:16 +00001952 size_type __bc = bucket_count();
1953 bool __inserted = false;
Eric Fiselier40492ba2016-07-23 20:36:55 +00001954 __next_pointer __nd;
Howard Hinnant3e519522010-05-11 19:42:16 +00001955 size_t __chash;
1956 if (__bc != 0)
1957 {
Howard Hinnant4cb38a82012-07-06 17:31:14 +00001958 __chash = __constrain_hash(__hash, __bc);
Howard Hinnant3e519522010-05-11 19:42:16 +00001959 __nd = __bucket_list_[__chash];
1960 if (__nd != nullptr)
1961 {
1962 for (__nd = __nd->__next_; __nd != nullptr &&
Eric Fiselier1a06fe52016-07-24 06:22:25 +00001963 (__nd->__hash() == __hash || __constrain_hash(__nd->__hash(), __bc) == __chash);
Howard Hinnant3e519522010-05-11 19:42:16 +00001964 __nd = __nd->__next_)
1965 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001966 if (key_eq()(__nd->__upcast()->__value_, __k))
Howard Hinnant3e519522010-05-11 19:42:16 +00001967 goto __done;
1968 }
1969 }
1970 }
1971 {
Eric Fiselierfcd02212016-02-11 11:59:44 +00001972#ifndef _LIBCPP_CXX03_LANG
1973 __node_holder __h = __construct_node_hash(__hash, _VSTD::forward<_Args>(__args)...);
1974#else
1975 __node_holder __h = __construct_node_hash(__hash, __args);
1976#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001977 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1978 {
Eric Fiselier9ba5c112015-02-02 21:31:48 +00001979 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnant3e519522010-05-11 19:42:16 +00001980 size_type(ceil(float(size() + 1) / max_load_factor()))));
1981 __bc = bucket_count();
Howard Hinnant4cb38a82012-07-06 17:31:14 +00001982 __chash = __constrain_hash(__hash, __bc);
Howard Hinnant3e519522010-05-11 19:42:16 +00001983 }
1984 // insert_after __bucket_list_[__chash], or __first_node if bucket is null
Eric Fiselier40492ba2016-07-23 20:36:55 +00001985 __next_pointer __pn = __bucket_list_[__chash];
Howard Hinnant3e519522010-05-11 19:42:16 +00001986 if (__pn == nullptr)
1987 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001988 __pn = __p1_.first().__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001989 __h->__next_ = __pn->__next_;
Eric Fiselier40492ba2016-07-23 20:36:55 +00001990 __pn->__next_ = __h.get()->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001991 // fix up __bucket_list_
1992 __bucket_list_[__chash] = __pn;
1993 if (__h->__next_ != nullptr)
Eric Fiselier40492ba2016-07-23 20:36:55 +00001994 __bucket_list_[__constrain_hash(__h->__next_->__hash(), __bc)]
1995 = __h.get()->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001996 }
1997 else
1998 {
1999 __h->__next_ = __pn->__next_;
Eric Fiselier40492ba2016-07-23 20:36:55 +00002000 __pn->__next_ = static_cast<__next_pointer>(__h.get());
Howard Hinnant3e519522010-05-11 19:42:16 +00002001 }
Eric Fiselier40492ba2016-07-23 20:36:55 +00002002 __nd = static_cast<__next_pointer>(__h.release());
Howard Hinnant3e519522010-05-11 19:42:16 +00002003 // increment size
2004 ++size();
2005 __inserted = true;
2006 }
2007__done:
Howard Hinnantb24c8022013-07-23 22:01:58 +00002008#if _LIBCPP_DEBUG_LEVEL >= 2
2009 return pair<iterator, bool>(iterator(__nd, this), __inserted);
2010#else
Howard Hinnant3e519522010-05-11 19:42:16 +00002011 return pair<iterator, bool>(iterator(__nd), __inserted);
Howard Hinnantb24c8022013-07-23 22:01:58 +00002012#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002013}
2014
Eric Fiselierfcd02212016-02-11 11:59:44 +00002015#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00002016
2017template <class _Tp, class _Hash, class _Equal, class _Alloc>
2018template <class... _Args>
2019pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
Duncan P. N. Exon Smithfde79b42016-03-17 20:45:20 +00002020__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_impl(_Args&&... __args)
Howard Hinnant3e519522010-05-11 19:42:16 +00002021{
Howard Hinnantce48a112011-06-30 21:18:19 +00002022 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnant3e519522010-05-11 19:42:16 +00002023 pair<iterator, bool> __r = __node_insert_unique(__h.get());
2024 if (__r.second)
2025 __h.release();
2026 return __r;
2027}
2028
2029template <class _Tp, class _Hash, class _Equal, class _Alloc>
2030template <class... _Args>
2031typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2032__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_multi(_Args&&... __args)
2033{
Howard Hinnantce48a112011-06-30 21:18:19 +00002034 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnant3e519522010-05-11 19:42:16 +00002035 iterator __r = __node_insert_multi(__h.get());
2036 __h.release();
2037 return __r;
2038}
2039
2040template <class _Tp, class _Hash, class _Equal, class _Alloc>
2041template <class... _Args>
2042typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2043__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_hint_multi(
2044 const_iterator __p, _Args&&... __args)
2045{
Howard Hinnante5c13de2013-07-29 19:05:47 +00002046#if _LIBCPP_DEBUG_LEVEL >= 2
2047 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2048 "unordered container::emplace_hint(const_iterator, args...) called with an iterator not"
2049 " referring to this unordered container");
2050#endif
Howard Hinnantce48a112011-06-30 21:18:19 +00002051 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnant3e519522010-05-11 19:42:16 +00002052 iterator __r = __node_insert_multi(__p, __h.get());
2053 __h.release();
2054 return __r;
2055}
2056
Eric Fiselierfcd02212016-02-11 11:59:44 +00002057#else // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00002058
2059template <class _Tp, class _Hash, class _Equal, class _Alloc>
2060typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
Eric Fiselierfcd02212016-02-11 11:59:44 +00002061__hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const __container_value_type& __x)
Howard Hinnant3e519522010-05-11 19:42:16 +00002062{
2063 __node_holder __h = __construct_node(__x);
2064 iterator __r = __node_insert_multi(__h.get());
2065 __h.release();
2066 return __r;
2067}
2068
2069template <class _Tp, class _Hash, class _Equal, class _Alloc>
2070typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2071__hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const_iterator __p,
Eric Fiselierfcd02212016-02-11 11:59:44 +00002072 const __container_value_type& __x)
Howard Hinnant3e519522010-05-11 19:42:16 +00002073{
Howard Hinnante5c13de2013-07-29 19:05:47 +00002074#if _LIBCPP_DEBUG_LEVEL >= 2
2075 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2076 "unordered container::insert(const_iterator, lvalue) called with an iterator not"
2077 " referring to this unordered container");
2078#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002079 __node_holder __h = __construct_node(__x);
2080 iterator __r = __node_insert_multi(__p, __h.get());
2081 __h.release();
2082 return __r;
2083}
2084
Eric Fiselierfcd02212016-02-11 11:59:44 +00002085#endif // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00002086
2087template <class _Tp, class _Hash, class _Equal, class _Alloc>
2088void
2089__hash_table<_Tp, _Hash, _Equal, _Alloc>::rehash(size_type __n)
2090{
Howard Hinnant4cb38a82012-07-06 17:31:14 +00002091 if (__n == 1)
2092 __n = 2;
2093 else if (__n & (__n - 1))
2094 __n = __next_prime(__n);
Howard Hinnant3e519522010-05-11 19:42:16 +00002095 size_type __bc = bucket_count();
2096 if (__n > __bc)
2097 __rehash(__n);
Howard Hinnant4cb38a82012-07-06 17:31:14 +00002098 else if (__n < __bc)
Howard Hinnant3e519522010-05-11 19:42:16 +00002099 {
Howard Hinnantce48a112011-06-30 21:18:19 +00002100 __n = _VSTD::max<size_type>
Howard Hinnant3e519522010-05-11 19:42:16 +00002101 (
2102 __n,
Eric Fiselier9ba5c112015-02-02 21:31:48 +00002103 __is_hash_power2(__bc) ? __next_hash_pow2(size_t(ceil(float(size()) / max_load_factor()))) :
2104 __next_prime(size_t(ceil(float(size()) / max_load_factor())))
Howard Hinnant3e519522010-05-11 19:42:16 +00002105 );
2106 if (__n < __bc)
2107 __rehash(__n);
2108 }
2109}
2110
2111template <class _Tp, class _Hash, class _Equal, class _Alloc>
2112void
2113__hash_table<_Tp, _Hash, _Equal, _Alloc>::__rehash(size_type __nbc)
2114{
Howard Hinnantb24c8022013-07-23 22:01:58 +00002115#if _LIBCPP_DEBUG_LEVEL >= 2
2116 __get_db()->__invalidate_all(this);
2117#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant3e519522010-05-11 19:42:16 +00002118 __pointer_allocator& __npa = __bucket_list_.get_deleter().__alloc();
2119 __bucket_list_.reset(__nbc > 0 ?
2120 __pointer_alloc_traits::allocate(__npa, __nbc) : nullptr);
2121 __bucket_list_.get_deleter().size() = __nbc;
2122 if (__nbc > 0)
2123 {
2124 for (size_type __i = 0; __i < __nbc; ++__i)
2125 __bucket_list_[__i] = nullptr;
Eric Fiselier40492ba2016-07-23 20:36:55 +00002126 __next_pointer __pp = __p1_.first().__ptr();
2127 __next_pointer __cp = __pp->__next_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002128 if (__cp != nullptr)
2129 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00002130 size_type __chash = __constrain_hash(__cp->__hash(), __nbc);
Howard Hinnant3e519522010-05-11 19:42:16 +00002131 __bucket_list_[__chash] = __pp;
2132 size_type __phash = __chash;
2133 for (__pp = __cp, __cp = __cp->__next_; __cp != nullptr;
2134 __cp = __pp->__next_)
2135 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00002136 __chash = __constrain_hash(__cp->__hash(), __nbc);
Howard Hinnant3e519522010-05-11 19:42:16 +00002137 if (__chash == __phash)
2138 __pp = __cp;
2139 else
2140 {
2141 if (__bucket_list_[__chash] == nullptr)
2142 {
2143 __bucket_list_[__chash] = __pp;
2144 __pp = __cp;
2145 __phash = __chash;
2146 }
2147 else
2148 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00002149 __next_pointer __np = __cp;
Howard Hinnant3e519522010-05-11 19:42:16 +00002150 for (; __np->__next_ != nullptr &&
Eric Fiselier40492ba2016-07-23 20:36:55 +00002151 key_eq()(__cp->__upcast()->__value_,
2152 __np->__next_->__upcast()->__value_);
Howard Hinnant3e519522010-05-11 19:42:16 +00002153 __np = __np->__next_)
2154 ;
2155 __pp->__next_ = __np->__next_;
2156 __np->__next_ = __bucket_list_[__chash]->__next_;
2157 __bucket_list_[__chash]->__next_ = __cp;
Howard Hinnantb3371f62010-08-22 00:02:43 +00002158
Howard Hinnant3e519522010-05-11 19:42:16 +00002159 }
2160 }
2161 }
2162 }
2163 }
2164}
2165
2166template <class _Tp, class _Hash, class _Equal, class _Alloc>
2167template <class _Key>
2168typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2169__hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k)
2170{
2171 size_t __hash = hash_function()(__k);
2172 size_type __bc = bucket_count();
2173 if (__bc != 0)
2174 {
Howard Hinnant4cb38a82012-07-06 17:31:14 +00002175 size_t __chash = __constrain_hash(__hash, __bc);
Eric Fiselier40492ba2016-07-23 20:36:55 +00002176 __next_pointer __nd = __bucket_list_[__chash];
Howard Hinnant3e519522010-05-11 19:42:16 +00002177 if (__nd != nullptr)
2178 {
2179 for (__nd = __nd->__next_; __nd != nullptr &&
Eric Fiselier40492ba2016-07-23 20:36:55 +00002180 (__nd->__hash() == __hash
2181 || __constrain_hash(__nd->__hash(), __bc) == __chash);
Howard Hinnant3e519522010-05-11 19:42:16 +00002182 __nd = __nd->__next_)
2183 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00002184 if ((__nd->__hash() == __hash)
2185 && key_eq()(__nd->__upcast()->__value_, __k))
Howard Hinnantb24c8022013-07-23 22:01:58 +00002186#if _LIBCPP_DEBUG_LEVEL >= 2
2187 return iterator(__nd, this);
2188#else
Howard Hinnant3e519522010-05-11 19:42:16 +00002189 return iterator(__nd);
Howard Hinnantb24c8022013-07-23 22:01:58 +00002190#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002191 }
2192 }
2193 }
2194 return end();
2195}
2196
2197template <class _Tp, class _Hash, class _Equal, class _Alloc>
2198template <class _Key>
2199typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
2200__hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k) const
2201{
2202 size_t __hash = hash_function()(__k);
2203 size_type __bc = bucket_count();
2204 if (__bc != 0)
2205 {
Howard Hinnant4cb38a82012-07-06 17:31:14 +00002206 size_t __chash = __constrain_hash(__hash, __bc);
Eric Fiselier40492ba2016-07-23 20:36:55 +00002207 __next_pointer __nd = __bucket_list_[__chash];
Howard Hinnant3e519522010-05-11 19:42:16 +00002208 if (__nd != nullptr)
2209 {
2210 for (__nd = __nd->__next_; __nd != nullptr &&
Eric Fiselier40492ba2016-07-23 20:36:55 +00002211 (__hash == __nd->__hash()
2212 || __constrain_hash(__nd->__hash(), __bc) == __chash);
Howard Hinnant3e519522010-05-11 19:42:16 +00002213 __nd = __nd->__next_)
2214 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00002215 if ((__nd->__hash() == __hash)
2216 && key_eq()(__nd->__upcast()->__value_, __k))
Howard Hinnantb24c8022013-07-23 22:01:58 +00002217#if _LIBCPP_DEBUG_LEVEL >= 2
2218 return const_iterator(__nd, this);
2219#else
Howard Hinnant3e519522010-05-11 19:42:16 +00002220 return const_iterator(__nd);
Howard Hinnantb24c8022013-07-23 22:01:58 +00002221#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002222 }
2223 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00002224
Howard Hinnant3e519522010-05-11 19:42:16 +00002225 }
2226 return end();
2227}
2228
Eric Fiselierfcd02212016-02-11 11:59:44 +00002229#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00002230
2231template <class _Tp, class _Hash, class _Equal, class _Alloc>
2232template <class ..._Args>
2233typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
2234__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(_Args&& ...__args)
2235{
Eric Fiselierfcd02212016-02-11 11:59:44 +00002236 static_assert(!__is_hash_value_type<_Args...>::value,
2237 "Construct cannot be called with a hash value type");
Howard Hinnant3e519522010-05-11 19:42:16 +00002238 __node_allocator& __na = __node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00002239 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselierfcd02212016-02-11 11:59:44 +00002240 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), _VSTD::forward<_Args>(__args)...);
Howard Hinnant3e519522010-05-11 19:42:16 +00002241 __h.get_deleter().__value_constructed = true;
2242 __h->__hash_ = hash_function()(__h->__value_);
2243 __h->__next_ = nullptr;
2244 return __h;
2245}
2246
2247template <class _Tp, class _Hash, class _Equal, class _Alloc>
Eric Fiselierfcd02212016-02-11 11:59:44 +00002248template <class _First, class ..._Rest>
Howard Hinnant3e519522010-05-11 19:42:16 +00002249typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Eric Fiselierfcd02212016-02-11 11:59:44 +00002250__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node_hash(
2251 size_t __hash, _First&& __f, _Rest&& ...__rest)
Howard Hinnant3e519522010-05-11 19:42:16 +00002252{
Eric Fiselierfcd02212016-02-11 11:59:44 +00002253 static_assert(!__is_hash_value_type<_First, _Rest...>::value,
2254 "Construct cannot be called with a hash value type");
Howard Hinnant3e519522010-05-11 19:42:16 +00002255 __node_allocator& __na = __node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00002256 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselierfcd02212016-02-11 11:59:44 +00002257 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_),
2258 _VSTD::forward<_First>(__f),
2259 _VSTD::forward<_Rest>(__rest)...);
Howard Hinnant3e519522010-05-11 19:42:16 +00002260 __h.get_deleter().__value_constructed = true;
2261 __h->__hash_ = __hash;
2262 __h->__next_ = nullptr;
Howard Hinnant179b1f82013-08-22 18:29:50 +00002263 return __h;
Howard Hinnant3e519522010-05-11 19:42:16 +00002264}
2265
Eric Fiselierfcd02212016-02-11 11:59:44 +00002266#else // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00002267
2268template <class _Tp, class _Hash, class _Equal, class _Alloc>
2269typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Eric Fiselierfcd02212016-02-11 11:59:44 +00002270__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(const __container_value_type& __v)
Howard Hinnant3e519522010-05-11 19:42:16 +00002271{
2272 __node_allocator& __na = __node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00002273 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselierfcd02212016-02-11 11:59:44 +00002274 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), __v);
Howard Hinnant3e519522010-05-11 19:42:16 +00002275 __h.get_deleter().__value_constructed = true;
2276 __h->__hash_ = hash_function()(__h->__value_);
2277 __h->__next_ = nullptr;
Dimitry Andric251c6292015-08-19 06:43:33 +00002278 return _LIBCPP_EXPLICIT_MOVE(__h); // explicitly moved for C++03
Howard Hinnant3e519522010-05-11 19:42:16 +00002279}
2280
Howard Hinnant3e519522010-05-11 19:42:16 +00002281template <class _Tp, class _Hash, class _Equal, class _Alloc>
2282typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Eric Fiselierfcd02212016-02-11 11:59:44 +00002283__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node_hash(size_t __hash,
2284 const __container_value_type& __v)
Howard Hinnant3e519522010-05-11 19:42:16 +00002285{
2286 __node_allocator& __na = __node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00002287 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselierfcd02212016-02-11 11:59:44 +00002288 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), __v);
Howard Hinnant3e519522010-05-11 19:42:16 +00002289 __h.get_deleter().__value_constructed = true;
2290 __h->__hash_ = __hash;
2291 __h->__next_ = nullptr;
Dimitry Andric251c6292015-08-19 06:43:33 +00002292 return _LIBCPP_EXPLICIT_MOVE(__h); // explicitly moved for C++03
Howard Hinnant3e519522010-05-11 19:42:16 +00002293}
2294
Eric Fiselierfcd02212016-02-11 11:59:44 +00002295#endif // _LIBCPP_CXX03_LANG
2296
Howard Hinnant3e519522010-05-11 19:42:16 +00002297template <class _Tp, class _Hash, class _Equal, class _Alloc>
2298typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2299__hash_table<_Tp, _Hash, _Equal, _Alloc>::erase(const_iterator __p)
2300{
Eric Fiselier40492ba2016-07-23 20:36:55 +00002301 __next_pointer __np = __p.__node_;
Howard Hinnantb24c8022013-07-23 22:01:58 +00002302#if _LIBCPP_DEBUG_LEVEL >= 2
2303 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2304 "unordered container erase(iterator) called with an iterator not"
2305 " referring to this container");
2306 _LIBCPP_ASSERT(__p != end(),
2307 "unordered container erase(iterator) called with a non-dereferenceable iterator");
2308 iterator __r(__np, this);
2309#else
Howard Hinnant3e519522010-05-11 19:42:16 +00002310 iterator __r(__np);
Howard Hinnantb24c8022013-07-23 22:01:58 +00002311#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002312 ++__r;
2313 remove(__p);
2314 return __r;
2315}
2316
2317template <class _Tp, class _Hash, class _Equal, class _Alloc>
2318typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2319__hash_table<_Tp, _Hash, _Equal, _Alloc>::erase(const_iterator __first,
2320 const_iterator __last)
2321{
Howard Hinnantb24c8022013-07-23 22:01:58 +00002322#if _LIBCPP_DEBUG_LEVEL >= 2
2323 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
2324 "unodered container::erase(iterator, iterator) called with an iterator not"
2325 " referring to this unodered container");
2326 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__last) == this,
2327 "unodered container::erase(iterator, iterator) called with an iterator not"
2328 " referring to this unodered container");
2329#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002330 for (const_iterator __p = __first; __first != __last; __p = __first)
2331 {
2332 ++__first;
2333 erase(__p);
2334 }
Eric Fiselier40492ba2016-07-23 20:36:55 +00002335 __next_pointer __np = __last.__node_;
Howard Hinnantb24c8022013-07-23 22:01:58 +00002336#if _LIBCPP_DEBUG_LEVEL >= 2
2337 return iterator (__np, this);
2338#else
Howard Hinnant3e519522010-05-11 19:42:16 +00002339 return iterator (__np);
Howard Hinnantb24c8022013-07-23 22:01:58 +00002340#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002341}
2342
2343template <class _Tp, class _Hash, class _Equal, class _Alloc>
2344template <class _Key>
2345typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2346__hash_table<_Tp, _Hash, _Equal, _Alloc>::__erase_unique(const _Key& __k)
2347{
2348 iterator __i = find(__k);
2349 if (__i == end())
2350 return 0;
2351 erase(__i);
2352 return 1;
2353}
2354
2355template <class _Tp, class _Hash, class _Equal, class _Alloc>
2356template <class _Key>
2357typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2358__hash_table<_Tp, _Hash, _Equal, _Alloc>::__erase_multi(const _Key& __k)
2359{
2360 size_type __r = 0;
2361 iterator __i = find(__k);
2362 if (__i != end())
2363 {
2364 iterator __e = end();
2365 do
2366 {
2367 erase(__i++);
2368 ++__r;
2369 } while (__i != __e && key_eq()(*__i, __k));
2370 }
2371 return __r;
2372}
2373
2374template <class _Tp, class _Hash, class _Equal, class _Alloc>
2375typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Howard Hinnant37141072011-06-04 18:54:24 +00002376__hash_table<_Tp, _Hash, _Equal, _Alloc>::remove(const_iterator __p) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00002377{
2378 // current node
Eric Fiselier40492ba2016-07-23 20:36:55 +00002379 __next_pointer __cn = __p.__node_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002380 size_type __bc = bucket_count();
Eric Fiselier40492ba2016-07-23 20:36:55 +00002381 size_t __chash = __constrain_hash(__cn->__hash(), __bc);
Howard Hinnant3e519522010-05-11 19:42:16 +00002382 // find previous node
Eric Fiselier40492ba2016-07-23 20:36:55 +00002383 __next_pointer __pn = __bucket_list_[__chash];
Howard Hinnant3e519522010-05-11 19:42:16 +00002384 for (; __pn->__next_ != __cn; __pn = __pn->__next_)
2385 ;
2386 // Fix up __bucket_list_
2387 // if __pn is not in same bucket (before begin is not in same bucket) &&
2388 // if __cn->__next_ is not in same bucket (nullptr is not in same bucket)
Eric Fiselier40492ba2016-07-23 20:36:55 +00002389 if (__pn == __p1_.first().__ptr()
2390 || __constrain_hash(__pn->__hash(), __bc) != __chash)
Howard Hinnant3e519522010-05-11 19:42:16 +00002391 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00002392 if (__cn->__next_ == nullptr
2393 || __constrain_hash(__cn->__next_->__hash(), __bc) != __chash)
Howard Hinnant3e519522010-05-11 19:42:16 +00002394 __bucket_list_[__chash] = nullptr;
2395 }
2396 // if __cn->__next_ is not in same bucket (nullptr is in same bucket)
2397 if (__cn->__next_ != nullptr)
2398 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00002399 size_t __nhash = __constrain_hash(__cn->__next_->__hash(), __bc);
Howard Hinnant3e519522010-05-11 19:42:16 +00002400 if (__nhash != __chash)
2401 __bucket_list_[__nhash] = __pn;
2402 }
2403 // remove __cn
2404 __pn->__next_ = __cn->__next_;
2405 __cn->__next_ = nullptr;
2406 --size();
Howard Hinnantb24c8022013-07-23 22:01:58 +00002407#if _LIBCPP_DEBUG_LEVEL >= 2
2408 __c_node* __c = __get_db()->__find_c_and_lock(this);
2409 for (__i_node** __p = __c->end_; __p != __c->beg_; )
2410 {
2411 --__p;
2412 iterator* __i = static_cast<iterator*>((*__p)->__i_);
2413 if (__i->__node_ == __cn)
2414 {
2415 (*__p)->__c_ = nullptr;
2416 if (--__c->end_ != __p)
2417 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
2418 }
2419 }
2420 __get_db()->unlock();
2421#endif
Eric Fiselier40492ba2016-07-23 20:36:55 +00002422 return __node_holder(__cn->__upcast(), _Dp(__node_alloc(), true));
Howard Hinnant3e519522010-05-11 19:42:16 +00002423}
2424
2425template <class _Tp, class _Hash, class _Equal, class _Alloc>
2426template <class _Key>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00002427inline
Howard Hinnant3e519522010-05-11 19:42:16 +00002428typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2429__hash_table<_Tp, _Hash, _Equal, _Alloc>::__count_unique(const _Key& __k) const
2430{
2431 return static_cast<size_type>(find(__k) != end());
2432}
2433
2434template <class _Tp, class _Hash, class _Equal, class _Alloc>
2435template <class _Key>
2436typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2437__hash_table<_Tp, _Hash, _Equal, _Alloc>::__count_multi(const _Key& __k) const
2438{
2439 size_type __r = 0;
2440 const_iterator __i = find(__k);
2441 if (__i != end())
2442 {
2443 const_iterator __e = end();
2444 do
2445 {
2446 ++__i;
2447 ++__r;
2448 } while (__i != __e && key_eq()(*__i, __k));
2449 }
2450 return __r;
2451}
2452
2453template <class _Tp, class _Hash, class _Equal, class _Alloc>
2454template <class _Key>
2455pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator,
2456 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator>
2457__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_unique(
2458 const _Key& __k)
2459{
2460 iterator __i = find(__k);
2461 iterator __j = __i;
2462 if (__i != end())
2463 ++__j;
2464 return pair<iterator, iterator>(__i, __j);
2465}
2466
2467template <class _Tp, class _Hash, class _Equal, class _Alloc>
2468template <class _Key>
2469pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator,
2470 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator>
2471__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_unique(
2472 const _Key& __k) const
2473{
2474 const_iterator __i = find(__k);
2475 const_iterator __j = __i;
2476 if (__i != end())
2477 ++__j;
2478 return pair<const_iterator, const_iterator>(__i, __j);
2479}
2480
2481template <class _Tp, class _Hash, class _Equal, class _Alloc>
2482template <class _Key>
2483pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator,
2484 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator>
2485__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_multi(
2486 const _Key& __k)
2487{
2488 iterator __i = find(__k);
2489 iterator __j = __i;
2490 if (__i != end())
2491 {
2492 iterator __e = end();
2493 do
2494 {
2495 ++__j;
2496 } while (__j != __e && key_eq()(*__j, __k));
2497 }
2498 return pair<iterator, iterator>(__i, __j);
2499}
2500
2501template <class _Tp, class _Hash, class _Equal, class _Alloc>
2502template <class _Key>
2503pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator,
2504 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator>
2505__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_multi(
2506 const _Key& __k) const
2507{
2508 const_iterator __i = find(__k);
2509 const_iterator __j = __i;
2510 if (__i != end())
2511 {
2512 const_iterator __e = end();
2513 do
2514 {
2515 ++__j;
2516 } while (__j != __e && key_eq()(*__j, __k));
2517 }
2518 return pair<const_iterator, const_iterator>(__i, __j);
2519}
2520
2521template <class _Tp, class _Hash, class _Equal, class _Alloc>
2522void
2523__hash_table<_Tp, _Hash, _Equal, _Alloc>::swap(__hash_table& __u)
Eric Fiselier87a82492015-07-18 20:40:46 +00002524#if _LIBCPP_STD_VER <= 11
Howard Hinnant37141072011-06-04 18:54:24 +00002525 _NOEXCEPT_(
Marshall Clowe3fbe142015-07-13 20:04:56 +00002526 __is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value
Marshall Clowe3fbe142015-07-13 20:04:56 +00002527 && (!allocator_traits<__pointer_allocator>::propagate_on_container_swap::value
2528 || __is_nothrow_swappable<__pointer_allocator>::value)
2529 && (!__node_traits::propagate_on_container_swap::value
2530 || __is_nothrow_swappable<__node_allocator>::value)
Marshall Clowe3fbe142015-07-13 20:04:56 +00002531 )
Eric Fiselier87a82492015-07-18 20:40:46 +00002532#else
2533 _NOEXCEPT_(__is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value)
2534#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002535{
2536 {
2537 __node_pointer_pointer __npp = __bucket_list_.release();
2538 __bucket_list_.reset(__u.__bucket_list_.release());
2539 __u.__bucket_list_.reset(__npp);
2540 }
Howard Hinnantce48a112011-06-30 21:18:19 +00002541 _VSTD::swap(__bucket_list_.get_deleter().size(), __u.__bucket_list_.get_deleter().size());
Marshall Clowe3fbe142015-07-13 20:04:56 +00002542 __swap_allocator(__bucket_list_.get_deleter().__alloc(),
Howard Hinnant3e519522010-05-11 19:42:16 +00002543 __u.__bucket_list_.get_deleter().__alloc());
Marshall Clowe3fbe142015-07-13 20:04:56 +00002544 __swap_allocator(__node_alloc(), __u.__node_alloc());
Howard Hinnantce48a112011-06-30 21:18:19 +00002545 _VSTD::swap(__p1_.first().__next_, __u.__p1_.first().__next_);
Howard Hinnant3e519522010-05-11 19:42:16 +00002546 __p2_.swap(__u.__p2_);
2547 __p3_.swap(__u.__p3_);
2548 if (size() > 0)
Eric Fiselier40492ba2016-07-23 20:36:55 +00002549 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash(), bucket_count())] =
2550 __p1_.first().__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00002551 if (__u.size() > 0)
Eric Fiselier40492ba2016-07-23 20:36:55 +00002552 __u.__bucket_list_[__constrain_hash(__u.__p1_.first().__next_->__hash(), __u.bucket_count())] =
2553 __u.__p1_.first().__ptr();
Howard Hinnantb24c8022013-07-23 22:01:58 +00002554#if _LIBCPP_DEBUG_LEVEL >= 2
2555 __get_db()->swap(this, &__u);
2556#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002557}
2558
2559template <class _Tp, class _Hash, class _Equal, class _Alloc>
2560typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2561__hash_table<_Tp, _Hash, _Equal, _Alloc>::bucket_size(size_type __n) const
2562{
Howard Hinnante5c13de2013-07-29 19:05:47 +00002563 _LIBCPP_ASSERT(__n < bucket_count(),
2564 "unordered container::bucket_size(n) called with n >= bucket_count()");
Eric Fiselier40492ba2016-07-23 20:36:55 +00002565 __next_pointer __np = __bucket_list_[__n];
Howard Hinnant3e519522010-05-11 19:42:16 +00002566 size_type __bc = bucket_count();
2567 size_type __r = 0;
2568 if (__np != nullptr)
2569 {
2570 for (__np = __np->__next_; __np != nullptr &&
Eric Fiselier40492ba2016-07-23 20:36:55 +00002571 __constrain_hash(__np->__hash(), __bc) == __n;
Howard Hinnant3e519522010-05-11 19:42:16 +00002572 __np = __np->__next_, ++__r)
2573 ;
2574 }
2575 return __r;
2576}
2577
Howard Hinnant37141072011-06-04 18:54:24 +00002578template <class _Tp, class _Hash, class _Equal, class _Alloc>
2579inline _LIBCPP_INLINE_VISIBILITY
2580void
2581swap(__hash_table<_Tp, _Hash, _Equal, _Alloc>& __x,
2582 __hash_table<_Tp, _Hash, _Equal, _Alloc>& __y)
2583 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
2584{
2585 __x.swap(__y);
2586}
2587
Howard Hinnantb24c8022013-07-23 22:01:58 +00002588#if _LIBCPP_DEBUG_LEVEL >= 2
2589
2590template <class _Tp, class _Hash, class _Equal, class _Alloc>
2591bool
2592__hash_table<_Tp, _Hash, _Equal, _Alloc>::__dereferenceable(const const_iterator* __i) const
2593{
2594 return __i->__node_ != nullptr;
2595}
2596
2597template <class _Tp, class _Hash, class _Equal, class _Alloc>
2598bool
2599__hash_table<_Tp, _Hash, _Equal, _Alloc>::__decrementable(const const_iterator*) const
2600{
2601 return false;
2602}
2603
2604template <class _Tp, class _Hash, class _Equal, class _Alloc>
2605bool
2606__hash_table<_Tp, _Hash, _Equal, _Alloc>::__addable(const const_iterator*, ptrdiff_t) const
2607{
2608 return false;
2609}
2610
2611template <class _Tp, class _Hash, class _Equal, class _Alloc>
2612bool
2613__hash_table<_Tp, _Hash, _Equal, _Alloc>::__subscriptable(const const_iterator*, ptrdiff_t) const
2614{
2615 return false;
2616}
2617
2618#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant3e519522010-05-11 19:42:16 +00002619_LIBCPP_END_NAMESPACE_STD
2620
2621#endif // _LIBCPP__HASH_TABLE