blob: 6f5b183105e665baf03e3dc41458a564a49a07a6 [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>
Eric Fiselier04333f92017-01-13 22:42:53 +000021#include <type_traits>
Howard Hinnant3e519522010-05-11 19:42:16 +000022
Eric Fiselierc1bd9192014-08-10 23:53:08 +000023#include <__debug>
Howard Hinnant42a30462013-08-02 00:26:35 +000024
Howard Hinnant073458b2011-10-17 20:05:10 +000025#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnant3e519522010-05-11 19:42:16 +000026#pragma GCC system_header
Howard Hinnant073458b2011-10-17 20:05:10 +000027#endif
Howard Hinnant3e519522010-05-11 19:42:16 +000028
Eric Fiseliera016efb2017-05-31 22:07:49 +000029_LIBCPP_PUSH_MACROS
30#include <__undef_macros>
Howard Hinnant3e519522010-05-11 19:42:16 +000031
Eric Fiselierfcd02212016-02-11 11:59:44 +000032
Eric Fiseliera016efb2017-05-31 22:07:49 +000033_LIBCPP_BEGIN_NAMESPACE_STD
34
Eric Fiselierfcd02212016-02-11 11:59:44 +000035template <class _Key, class _Tp>
36struct __hash_value_type;
Eric Fiselierfcd02212016-02-11 11:59:44 +000037
38#ifndef _LIBCPP_CXX03_LANG
39template <class _Tp>
40struct __is_hash_value_type_imp : false_type {};
41
42template <class _Key, class _Value>
43struct __is_hash_value_type_imp<__hash_value_type<_Key, _Value>> : true_type {};
44
45template <class ..._Args>
46struct __is_hash_value_type : false_type {};
47
48template <class _One>
49struct __is_hash_value_type<_One> : __is_hash_value_type_imp<typename __uncvref<_One>::type> {};
50#endif
51
Howard Hinnant6e412562013-03-06 23:30:19 +000052_LIBCPP_FUNC_VIS
Howard Hinnantce534202011-06-14 19:58:17 +000053size_t __next_prime(size_t __n);
Howard Hinnant3e519522010-05-11 19:42:16 +000054
55template <class _NodePtr>
56struct __hash_node_base
57{
Eric Fiselier40492ba2016-07-23 20:36:55 +000058 typedef typename pointer_traits<_NodePtr>::element_type __node_type;
Howard Hinnant3e519522010-05-11 19:42:16 +000059 typedef __hash_node_base __first_node;
Eric Fiselier40492ba2016-07-23 20:36:55 +000060 typedef typename __rebind_pointer<_NodePtr, __first_node>::type __node_base_pointer;
61 typedef _NodePtr __node_pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +000062
Eric Fiselier40492ba2016-07-23 20:36:55 +000063#if defined(_LIBCPP_ABI_FIX_UNORDERED_NODE_POINTER_UB)
64 typedef __node_base_pointer __next_pointer;
65#else
66 typedef typename conditional<
67 is_pointer<__node_pointer>::value,
68 __node_base_pointer,
69 __node_pointer>::type __next_pointer;
70#endif
71
72 __next_pointer __next_;
73
74 _LIBCPP_INLINE_VISIBILITY
75 __next_pointer __ptr() _NOEXCEPT {
76 return static_cast<__next_pointer>(
77 pointer_traits<__node_base_pointer>::pointer_to(*this));
78 }
79
80 _LIBCPP_INLINE_VISIBILITY
81 __node_pointer __upcast() _NOEXCEPT {
82 return static_cast<__node_pointer>(
83 pointer_traits<__node_base_pointer>::pointer_to(*this));
84 }
85
86 _LIBCPP_INLINE_VISIBILITY
87 size_t __hash() const _NOEXCEPT {
88 return static_cast<__node_type const&>(*this).__hash_;
89 }
Howard Hinnant3e519522010-05-11 19:42:16 +000090
Howard Hinnant37141072011-06-04 18:54:24 +000091 _LIBCPP_INLINE_VISIBILITY __hash_node_base() _NOEXCEPT : __next_(nullptr) {}
Howard Hinnant3e519522010-05-11 19:42:16 +000092};
93
94template <class _Tp, class _VoidPtr>
95struct __hash_node
96 : public __hash_node_base
97 <
Eric Fiselier934b0922015-12-30 21:52:00 +000098 typename __rebind_pointer<_VoidPtr, __hash_node<_Tp, _VoidPtr> >::type
Howard Hinnant3e519522010-05-11 19:42:16 +000099 >
100{
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000101 typedef _Tp __node_value_type;
Howard Hinnant3e519522010-05-11 19:42:16 +0000102
Eric Fiselier40492ba2016-07-23 20:36:55 +0000103 size_t __hash_;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000104 __node_value_type __value_;
Howard Hinnant3e519522010-05-11 19:42:16 +0000105};
106
Howard Hinnant4cb38a82012-07-06 17:31:14 +0000107inline _LIBCPP_INLINE_VISIBILITY
108bool
Eric Fiselier9ba5c112015-02-02 21:31:48 +0000109__is_hash_power2(size_t __bc)
Howard Hinnant4cb38a82012-07-06 17:31:14 +0000110{
111 return __bc > 2 && !(__bc & (__bc - 1));
112}
113
114inline _LIBCPP_INLINE_VISIBILITY
115size_t
116__constrain_hash(size_t __h, size_t __bc)
117{
Eric Fiselier118cb412016-07-11 22:02:02 +0000118 return !(__bc & (__bc - 1)) ? __h & (__bc - 1) :
119 (__h < __bc ? __h : __h % __bc);
Howard Hinnant4cb38a82012-07-06 17:31:14 +0000120}
121
122inline _LIBCPP_INLINE_VISIBILITY
123size_t
Eric Fiselier9ba5c112015-02-02 21:31:48 +0000124__next_hash_pow2(size_t __n)
Howard Hinnant4cb38a82012-07-06 17:31:14 +0000125{
Marshall Clow87af6462017-06-03 00:08:32 +0000126 return __n < 2 ? __n : (size_t(1) << (std::numeric_limits<size_t>::digits - __clz(__n-1)));
Howard Hinnant4cb38a82012-07-06 17:31:14 +0000127}
128
Duncan P. N. Exon Smithfde79b42016-03-17 20:45:20 +0000129
Howard Hinnantce534202011-06-14 19:58:17 +0000130template <class _Tp, class _Hash, class _Equal, class _Alloc> class __hash_table;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000131
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +0000132template <class _NodePtr> class _LIBCPP_TEMPLATE_VIS __hash_iterator;
133template <class _ConstNodePtr> class _LIBCPP_TEMPLATE_VIS __hash_const_iterator;
134template <class _NodePtr> class _LIBCPP_TEMPLATE_VIS __hash_local_iterator;
135template <class _ConstNodePtr> class _LIBCPP_TEMPLATE_VIS __hash_const_local_iterator;
136template <class _HashIterator> class _LIBCPP_TEMPLATE_VIS __hash_map_iterator;
137template <class _HashIterator> class _LIBCPP_TEMPLATE_VIS __hash_map_const_iterator;
Howard Hinnant3e519522010-05-11 19:42:16 +0000138
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000139template <class _Tp>
Eric Fiselier43b121d2016-02-20 07:59:16 +0000140struct __hash_key_value_types {
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000141 static_assert(!is_reference<_Tp>::value && !is_const<_Tp>::value, "");
142 typedef _Tp key_type;
143 typedef _Tp __node_value_type;
144 typedef _Tp __container_value_type;
145 static const bool __is_map = false;
Eric Fiselierfcd02212016-02-11 11:59:44 +0000146
147 _LIBCPP_INLINE_VISIBILITY
148 static key_type const& __get_key(_Tp const& __v) {
149 return __v;
150 }
151 _LIBCPP_INLINE_VISIBILITY
152 static __container_value_type const& __get_value(__node_value_type const& __v) {
153 return __v;
154 }
155 _LIBCPP_INLINE_VISIBILITY
156 static __container_value_type* __get_ptr(__node_value_type& __n) {
157 return _VSTD::addressof(__n);
158 }
159#ifndef _LIBCPP_CXX03_LANG
160 _LIBCPP_INLINE_VISIBILITY
Erik Pilkingtonf52318b2018-06-04 20:38:23 +0000161 static __container_value_type&& __move(__node_value_type& __v) {
Eric Fiselierfcd02212016-02-11 11:59:44 +0000162 return _VSTD::move(__v);
163 }
164#endif
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000165};
166
167template <class _Key, class _Tp>
Eric Fiselier43b121d2016-02-20 07:59:16 +0000168struct __hash_key_value_types<__hash_value_type<_Key, _Tp> > {
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000169 typedef _Key key_type;
170 typedef _Tp mapped_type;
171 typedef __hash_value_type<_Key, _Tp> __node_value_type;
172 typedef pair<const _Key, _Tp> __container_value_type;
173 typedef __container_value_type __map_value_type;
174 static const bool __is_map = true;
Eric Fiselierfcd02212016-02-11 11:59:44 +0000175
176 _LIBCPP_INLINE_VISIBILITY
177 static key_type const& __get_key(__container_value_type const& __v) {
178 return __v.first;
179 }
180
181 template <class _Up>
182 _LIBCPP_INLINE_VISIBILITY
183 static typename enable_if<__is_same_uncvref<_Up, __node_value_type>::value,
184 __container_value_type const&>::type
185 __get_value(_Up& __t) {
Erik Pilkingtonf52318b2018-06-04 20:38:23 +0000186 return __t.__get_value();
Eric Fiselierfcd02212016-02-11 11:59:44 +0000187 }
188
189 template <class _Up>
190 _LIBCPP_INLINE_VISIBILITY
191 static typename enable_if<__is_same_uncvref<_Up, __container_value_type>::value,
192 __container_value_type const&>::type
193 __get_value(_Up& __t) {
194 return __t;
195 }
196
197 _LIBCPP_INLINE_VISIBILITY
198 static __container_value_type* __get_ptr(__node_value_type& __n) {
Erik Pilkingtonf52318b2018-06-04 20:38:23 +0000199 return _VSTD::addressof(__n.__get_value());
Eric Fiselierfcd02212016-02-11 11:59:44 +0000200 }
201#ifndef _LIBCPP_CXX03_LANG
202 _LIBCPP_INLINE_VISIBILITY
Erik Pilkingtonf52318b2018-06-04 20:38:23 +0000203 static pair<key_type&&, mapped_type&&> __move(__node_value_type& __v) {
204 return __v.__move();
Eric Fiselierfcd02212016-02-11 11:59:44 +0000205 }
206#endif
207
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000208};
209
Eric Fiselier43b121d2016-02-20 07:59:16 +0000210template <class _Tp, class _AllocPtr, class _KVTypes = __hash_key_value_types<_Tp>,
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000211 bool = _KVTypes::__is_map>
Eric Fiselier43b121d2016-02-20 07:59:16 +0000212struct __hash_map_pointer_types {};
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000213
214template <class _Tp, class _AllocPtr, class _KVTypes>
Eric Fiselier43b121d2016-02-20 07:59:16 +0000215struct __hash_map_pointer_types<_Tp, _AllocPtr, _KVTypes, true> {
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000216 typedef typename _KVTypes::__map_value_type _Mv;
217 typedef typename __rebind_pointer<_AllocPtr, _Mv>::type
218 __map_value_type_pointer;
219 typedef typename __rebind_pointer<_AllocPtr, const _Mv>::type
220 __const_map_value_type_pointer;
221};
222
223template <class _NodePtr, class _NodeT = typename pointer_traits<_NodePtr>::element_type>
224struct __hash_node_types;
225
226template <class _NodePtr, class _Tp, class _VoidPtr>
227struct __hash_node_types<_NodePtr, __hash_node<_Tp, _VoidPtr> >
Eric Fiselier43b121d2016-02-20 07:59:16 +0000228 : public __hash_key_value_types<_Tp>, __hash_map_pointer_types<_Tp, _VoidPtr>
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000229
230{
Eric Fiselier43b121d2016-02-20 07:59:16 +0000231 typedef __hash_key_value_types<_Tp> __base;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000232
233public:
234 typedef ptrdiff_t difference_type;
235 typedef size_t size_type;
236
237 typedef typename __rebind_pointer<_NodePtr, void>::type __void_pointer;
238
239 typedef typename pointer_traits<_NodePtr>::element_type __node_type;
240 typedef _NodePtr __node_pointer;
241
242 typedef __hash_node_base<__node_pointer> __node_base_type;
243 typedef typename __rebind_pointer<_NodePtr, __node_base_type>::type
244 __node_base_pointer;
245
Eric Fiselier40492ba2016-07-23 20:36:55 +0000246 typedef typename __node_base_type::__next_pointer __next_pointer;
247
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000248 typedef _Tp __node_value_type;
249 typedef typename __rebind_pointer<_VoidPtr, __node_value_type>::type
250 __node_value_type_pointer;
251 typedef typename __rebind_pointer<_VoidPtr, const __node_value_type>::type
252 __const_node_value_type_pointer;
Eric Fiselier40492ba2016-07-23 20:36:55 +0000253
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000254private:
255 static_assert(!is_const<__node_type>::value,
256 "_NodePtr should never be a pointer to const");
257 static_assert((is_same<typename pointer_traits<_VoidPtr>::element_type, void>::value),
258 "_VoidPtr does not point to unqualified void type");
259 static_assert((is_same<typename __rebind_pointer<_VoidPtr, __node_type>::type,
260 _NodePtr>::value), "_VoidPtr does not rebind to _NodePtr.");
261};
262
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000263template <class _HashIterator>
264struct __hash_node_types_from_iterator;
265template <class _NodePtr>
266struct __hash_node_types_from_iterator<__hash_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
267template <class _NodePtr>
268struct __hash_node_types_from_iterator<__hash_const_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
269template <class _NodePtr>
270struct __hash_node_types_from_iterator<__hash_local_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
271template <class _NodePtr>
272struct __hash_node_types_from_iterator<__hash_const_local_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
273
274
275template <class _NodeValueTp, class _VoidPtr>
276struct __make_hash_node_types {
277 typedef __hash_node<_NodeValueTp, _VoidPtr> _NodeTp;
278 typedef typename __rebind_pointer<_VoidPtr, _NodeTp>::type _NodePtr;
279 typedef __hash_node_types<_NodePtr> type;
280};
281
Howard Hinnant3e519522010-05-11 19:42:16 +0000282template <class _NodePtr>
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +0000283class _LIBCPP_TEMPLATE_VIS __hash_iterator
Howard Hinnant3e519522010-05-11 19:42:16 +0000284{
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000285 typedef __hash_node_types<_NodePtr> _NodeTypes;
Eric Fiselier40492ba2016-07-23 20:36:55 +0000286 typedef _NodePtr __node_pointer;
287 typedef typename _NodeTypes::__next_pointer __next_pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000288
Eric Fiselier40492ba2016-07-23 20:36:55 +0000289 __next_pointer __node_;
Howard Hinnant3e519522010-05-11 19:42:16 +0000290
291public:
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000292 typedef forward_iterator_tag iterator_category;
293 typedef typename _NodeTypes::__node_value_type value_type;
294 typedef typename _NodeTypes::difference_type difference_type;
295 typedef value_type& reference;
296 typedef typename _NodeTypes::__node_value_type_pointer pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000297
Eric Fiselier40492ba2016-07-23 20:36:55 +0000298 _LIBCPP_INLINE_VISIBILITY __hash_iterator() _NOEXCEPT : __node_(nullptr) {
299 _LIBCPP_DEBUG_MODE(__get_db()->__insert_i(this));
Howard Hinnantb24c8022013-07-23 22:01:58 +0000300 }
301
302#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant43d99232010-09-21 17:32:39 +0000303 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb24c8022013-07-23 22:01:58 +0000304 __hash_iterator(const __hash_iterator& __i)
305 : __node_(__i.__node_)
306 {
307 __get_db()->__iterator_copy(this, &__i);
308 }
309
Howard Hinnant43d99232010-09-21 17:32:39 +0000310 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb24c8022013-07-23 22:01:58 +0000311 ~__hash_iterator()
312 {
313 __get_db()->__erase_i(this);
314 }
315
316 _LIBCPP_INLINE_VISIBILITY
317 __hash_iterator& operator=(const __hash_iterator& __i)
318 {
319 if (this != &__i)
320 {
321 __get_db()->__iterator_copy(this, &__i);
322 __node_ = __i.__node_;
323 }
324 return *this;
325 }
Howard Hinnantb24c8022013-07-23 22:01:58 +0000326#endif // _LIBCPP_DEBUG_LEVEL >= 2
327
328 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000329 reference operator*() const {
330 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
331 "Attempted to dereference a non-dereferenceable unordered container iterator");
332 return __node_->__upcast()->__value_;
333 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000334
Howard Hinnant43d99232010-09-21 17:32:39 +0000335 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000336 pointer operator->() const {
337 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
338 "Attempted to dereference a non-dereferenceable unordered container iterator");
339 return pointer_traits<pointer>::pointer_to(__node_->__upcast()->__value_);
340 }
341
342 _LIBCPP_INLINE_VISIBILITY
343 __hash_iterator& operator++() {
344 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
Howard Hinnantb24c8022013-07-23 22:01:58 +0000345 "Attempted to increment non-incrementable unordered container iterator");
Howard Hinnant3e519522010-05-11 19:42:16 +0000346 __node_ = __node_->__next_;
347 return *this;
348 }
349
Howard Hinnant43d99232010-09-21 17:32:39 +0000350 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000351 __hash_iterator operator++(int)
352 {
353 __hash_iterator __t(*this);
354 ++(*this);
355 return __t;
356 }
357
Howard Hinnant43d99232010-09-21 17:32:39 +0000358 friend _LIBCPP_INLINE_VISIBILITY
359 bool operator==(const __hash_iterator& __x, const __hash_iterator& __y)
Howard Hinnantb24c8022013-07-23 22:01:58 +0000360 {
Howard Hinnantb24c8022013-07-23 22:01:58 +0000361 return __x.__node_ == __y.__node_;
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 {return !(__x == __y);}
Howard Hinnant3e519522010-05-11 19:42:16 +0000366
367private:
Howard Hinnantb24c8022013-07-23 22:01:58 +0000368#if _LIBCPP_DEBUG_LEVEL >= 2
369 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000370 __hash_iterator(__next_pointer __node, const void* __c) _NOEXCEPT
Howard Hinnantb24c8022013-07-23 22:01:58 +0000371 : __node_(__node)
372 {
373 __get_db()->__insert_ic(this, __c);
374 }
375#else
Howard Hinnant43d99232010-09-21 17:32:39 +0000376 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000377 __hash_iterator(__next_pointer __node) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000378 : __node_(__node)
379 {}
Howard Hinnantb24c8022013-07-23 22:01:58 +0000380#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000381 template <class, class, class, class> friend class __hash_table;
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +0000382 template <class> friend class _LIBCPP_TEMPLATE_VIS __hash_const_iterator;
383 template <class> friend class _LIBCPP_TEMPLATE_VIS __hash_map_iterator;
384 template <class, class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS unordered_map;
385 template <class, class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS unordered_multimap;
Howard Hinnant3e519522010-05-11 19:42:16 +0000386};
387
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000388template <class _NodePtr>
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +0000389class _LIBCPP_TEMPLATE_VIS __hash_const_iterator
Howard Hinnant3e519522010-05-11 19:42:16 +0000390{
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000391 static_assert(!is_const<typename pointer_traits<_NodePtr>::element_type>::value, "");
392 typedef __hash_node_types<_NodePtr> _NodeTypes;
Eric Fiselier40492ba2016-07-23 20:36:55 +0000393 typedef _NodePtr __node_pointer;
394 typedef typename _NodeTypes::__next_pointer __next_pointer;
Eric Fiselier757373e2016-02-18 00:20:34 +0000395
Eric Fiselier40492ba2016-07-23 20:36:55 +0000396 __next_pointer __node_;
Howard Hinnant3e519522010-05-11 19:42:16 +0000397
398public:
Eric Fiselier757373e2016-02-18 00:20:34 +0000399 typedef __hash_iterator<_NodePtr> __non_const_iterator;
400
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000401 typedef forward_iterator_tag iterator_category;
402 typedef typename _NodeTypes::__node_value_type value_type;
403 typedef typename _NodeTypes::difference_type difference_type;
404 typedef const value_type& reference;
405 typedef typename _NodeTypes::__const_node_value_type_pointer pointer;
406
Howard Hinnant3e519522010-05-11 19:42:16 +0000407
Eric Fiselier40492ba2016-07-23 20:36:55 +0000408 _LIBCPP_INLINE_VISIBILITY __hash_const_iterator() _NOEXCEPT : __node_(nullptr) {
409 _LIBCPP_DEBUG_MODE(__get_db()->__insert_i(this));
Howard Hinnantb24c8022013-07-23 22:01:58 +0000410 }
Eric Fiselier40492ba2016-07-23 20:36:55 +0000411
Louis Dionne3560fbf32018-12-06 21:46:17 +0000412 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +0000413 __hash_const_iterator(const __non_const_iterator& __x) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000414 : __node_(__x.__node_)
Howard Hinnantb24c8022013-07-23 22:01:58 +0000415 {
Eric Fiselier40492ba2016-07-23 20:36:55 +0000416 _LIBCPP_DEBUG_MODE(__get_db()->__iterator_copy(this, &__x));
Howard Hinnantb24c8022013-07-23 22:01:58 +0000417 }
418
419#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant43d99232010-09-21 17:32:39 +0000420 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb24c8022013-07-23 22:01:58 +0000421 __hash_const_iterator(const __hash_const_iterator& __i)
422 : __node_(__i.__node_)
423 {
424 __get_db()->__iterator_copy(this, &__i);
425 }
426
Howard Hinnant43d99232010-09-21 17:32:39 +0000427 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb24c8022013-07-23 22:01:58 +0000428 ~__hash_const_iterator()
429 {
430 __get_db()->__erase_i(this);
431 }
432
433 _LIBCPP_INLINE_VISIBILITY
434 __hash_const_iterator& operator=(const __hash_const_iterator& __i)
435 {
436 if (this != &__i)
437 {
438 __get_db()->__iterator_copy(this, &__i);
439 __node_ = __i.__node_;
440 }
441 return *this;
442 }
Howard Hinnantb24c8022013-07-23 22:01:58 +0000443#endif // _LIBCPP_DEBUG_LEVEL >= 2
444
445 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000446 reference operator*() const {
447 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
Howard Hinnantb24c8022013-07-23 22:01:58 +0000448 "Attempted to dereference a non-dereferenceable unordered container const_iterator");
Eric Fiselier40492ba2016-07-23 20:36:55 +0000449 return __node_->__upcast()->__value_;
450 }
Howard Hinnantb24c8022013-07-23 22:01:58 +0000451 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000452 pointer operator->() const {
453 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
Howard Hinnantb24c8022013-07-23 22:01:58 +0000454 "Attempted to dereference a non-dereferenceable unordered container const_iterator");
Eric Fiselier40492ba2016-07-23 20:36:55 +0000455 return pointer_traits<pointer>::pointer_to(__node_->__upcast()->__value_);
456 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000457
Howard Hinnant43d99232010-09-21 17:32:39 +0000458 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000459 __hash_const_iterator& operator++() {
460 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
461 "Attempted to increment non-incrementable unordered container const_iterator");
Howard Hinnant3e519522010-05-11 19:42:16 +0000462 __node_ = __node_->__next_;
463 return *this;
464 }
465
Howard Hinnant43d99232010-09-21 17:32:39 +0000466 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000467 __hash_const_iterator operator++(int)
468 {
469 __hash_const_iterator __t(*this);
470 ++(*this);
471 return __t;
472 }
473
Howard Hinnant43d99232010-09-21 17:32:39 +0000474 friend _LIBCPP_INLINE_VISIBILITY
475 bool operator==(const __hash_const_iterator& __x, const __hash_const_iterator& __y)
Howard Hinnantb24c8022013-07-23 22:01:58 +0000476 {
Howard Hinnantb24c8022013-07-23 22:01:58 +0000477 return __x.__node_ == __y.__node_;
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 {return !(__x == __y);}
Howard Hinnant3e519522010-05-11 19:42:16 +0000482
483private:
Howard Hinnantb24c8022013-07-23 22:01:58 +0000484#if _LIBCPP_DEBUG_LEVEL >= 2
485 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000486 __hash_const_iterator(__next_pointer __node, const void* __c) _NOEXCEPT
Howard Hinnantb24c8022013-07-23 22:01:58 +0000487 : __node_(__node)
488 {
489 __get_db()->__insert_ic(this, __c);
490 }
491#else
Howard Hinnant43d99232010-09-21 17:32:39 +0000492 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000493 __hash_const_iterator(__next_pointer __node) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000494 : __node_(__node)
495 {}
Howard Hinnantb24c8022013-07-23 22:01:58 +0000496#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000497 template <class, class, class, class> friend class __hash_table;
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +0000498 template <class> friend class _LIBCPP_TEMPLATE_VIS __hash_map_const_iterator;
499 template <class, class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS unordered_map;
500 template <class, class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS unordered_multimap;
Howard Hinnant3e519522010-05-11 19:42:16 +0000501};
502
Howard Hinnant3e519522010-05-11 19:42:16 +0000503template <class _NodePtr>
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +0000504class _LIBCPP_TEMPLATE_VIS __hash_local_iterator
Howard Hinnant3e519522010-05-11 19:42:16 +0000505{
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000506 typedef __hash_node_types<_NodePtr> _NodeTypes;
Eric Fiselier40492ba2016-07-23 20:36:55 +0000507 typedef _NodePtr __node_pointer;
508 typedef typename _NodeTypes::__next_pointer __next_pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000509
Eric Fiselier40492ba2016-07-23 20:36:55 +0000510 __next_pointer __node_;
Howard Hinnant3e519522010-05-11 19:42:16 +0000511 size_t __bucket_;
512 size_t __bucket_count_;
513
Howard Hinnant3e519522010-05-11 19:42:16 +0000514public:
515 typedef forward_iterator_tag iterator_category;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000516 typedef typename _NodeTypes::__node_value_type value_type;
517 typedef typename _NodeTypes::difference_type difference_type;
Howard Hinnant3e519522010-05-11 19:42:16 +0000518 typedef value_type& reference;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000519 typedef typename _NodeTypes::__node_value_type_pointer pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000520
Eric Fiselier40492ba2016-07-23 20:36:55 +0000521 _LIBCPP_INLINE_VISIBILITY __hash_local_iterator() _NOEXCEPT : __node_(nullptr) {
522 _LIBCPP_DEBUG_MODE(__get_db()->__insert_i(this));
Howard Hinnantb24c8022013-07-23 22:01:58 +0000523 }
524
525#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant43d99232010-09-21 17:32:39 +0000526 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb24c8022013-07-23 22:01:58 +0000527 __hash_local_iterator(const __hash_local_iterator& __i)
528 : __node_(__i.__node_),
529 __bucket_(__i.__bucket_),
530 __bucket_count_(__i.__bucket_count_)
531 {
532 __get_db()->__iterator_copy(this, &__i);
533 }
534
Howard Hinnant43d99232010-09-21 17:32:39 +0000535 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb24c8022013-07-23 22:01:58 +0000536 ~__hash_local_iterator()
537 {
538 __get_db()->__erase_i(this);
539 }
540
541 _LIBCPP_INLINE_VISIBILITY
542 __hash_local_iterator& operator=(const __hash_local_iterator& __i)
543 {
544 if (this != &__i)
545 {
546 __get_db()->__iterator_copy(this, &__i);
547 __node_ = __i.__node_;
548 __bucket_ = __i.__bucket_;
549 __bucket_count_ = __i.__bucket_count_;
550 }
551 return *this;
552 }
Howard Hinnantb24c8022013-07-23 22:01:58 +0000553#endif // _LIBCPP_DEBUG_LEVEL >= 2
554
555 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000556 reference operator*() const {
557 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
Howard Hinnantb24c8022013-07-23 22:01:58 +0000558 "Attempted to dereference a non-dereferenceable unordered container local_iterator");
Eric Fiselier40492ba2016-07-23 20:36:55 +0000559 return __node_->__upcast()->__value_;
560 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000561
Howard Hinnant43d99232010-09-21 17:32:39 +0000562 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000563 pointer operator->() const {
564 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
565 "Attempted to dereference a non-dereferenceable unordered container local_iterator");
566 return pointer_traits<pointer>::pointer_to(__node_->__upcast()->__value_);
567 }
568
569 _LIBCPP_INLINE_VISIBILITY
570 __hash_local_iterator& operator++() {
571 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
Howard Hinnantb24c8022013-07-23 22:01:58 +0000572 "Attempted to increment non-incrementable unordered container local_iterator");
Howard Hinnant3e519522010-05-11 19:42:16 +0000573 __node_ = __node_->__next_;
Eric Fiselier40492ba2016-07-23 20:36:55 +0000574 if (__node_ != nullptr && __constrain_hash(__node_->__hash(), __bucket_count_) != __bucket_)
Howard Hinnant3e519522010-05-11 19:42:16 +0000575 __node_ = nullptr;
576 return *this;
577 }
578
Howard Hinnant43d99232010-09-21 17:32:39 +0000579 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000580 __hash_local_iterator operator++(int)
581 {
582 __hash_local_iterator __t(*this);
583 ++(*this);
584 return __t;
585 }
586
Howard Hinnant43d99232010-09-21 17:32:39 +0000587 friend _LIBCPP_INLINE_VISIBILITY
588 bool operator==(const __hash_local_iterator& __x, const __hash_local_iterator& __y)
Howard Hinnantb24c8022013-07-23 22:01:58 +0000589 {
Howard Hinnantb24c8022013-07-23 22:01:58 +0000590 return __x.__node_ == __y.__node_;
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 {return !(__x == __y);}
Howard Hinnant3e519522010-05-11 19:42:16 +0000595
596private:
Howard Hinnantb24c8022013-07-23 22:01:58 +0000597#if _LIBCPP_DEBUG_LEVEL >= 2
598 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000599 __hash_local_iterator(__next_pointer __node, size_t __bucket,
Howard Hinnantb24c8022013-07-23 22:01:58 +0000600 size_t __bucket_count, const void* __c) _NOEXCEPT
601 : __node_(__node),
602 __bucket_(__bucket),
603 __bucket_count_(__bucket_count)
604 {
605 __get_db()->__insert_ic(this, __c);
606 if (__node_ != nullptr)
607 __node_ = __node_->__next_;
608 }
609#else
Howard Hinnant43d99232010-09-21 17:32:39 +0000610 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000611 __hash_local_iterator(__next_pointer __node, size_t __bucket,
Howard Hinnant37141072011-06-04 18:54:24 +0000612 size_t __bucket_count) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000613 : __node_(__node),
614 __bucket_(__bucket),
615 __bucket_count_(__bucket_count)
616 {
617 if (__node_ != nullptr)
618 __node_ = __node_->__next_;
619 }
Howard Hinnantb24c8022013-07-23 22:01:58 +0000620#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000621 template <class, class, class, class> friend class __hash_table;
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +0000622 template <class> friend class _LIBCPP_TEMPLATE_VIS __hash_const_local_iterator;
623 template <class> friend class _LIBCPP_TEMPLATE_VIS __hash_map_iterator;
Howard Hinnant3e519522010-05-11 19:42:16 +0000624};
625
626template <class _ConstNodePtr>
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +0000627class _LIBCPP_TEMPLATE_VIS __hash_const_local_iterator
Howard Hinnant3e519522010-05-11 19:42:16 +0000628{
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000629 typedef __hash_node_types<_ConstNodePtr> _NodeTypes;
Eric Fiselier40492ba2016-07-23 20:36:55 +0000630 typedef _ConstNodePtr __node_pointer;
631 typedef typename _NodeTypes::__next_pointer __next_pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000632
Eric Fiselier40492ba2016-07-23 20:36:55 +0000633 __next_pointer __node_;
Howard Hinnant3e519522010-05-11 19:42:16 +0000634 size_t __bucket_;
635 size_t __bucket_count_;
636
637 typedef pointer_traits<__node_pointer> __pointer_traits;
638 typedef typename __pointer_traits::element_type __node;
639 typedef typename remove_const<__node>::type __non_const_node;
Eric Fiselier934b0922015-12-30 21:52:00 +0000640 typedef typename __rebind_pointer<__node_pointer, __non_const_node>::type
641 __non_const_node_pointer;
Eric Fiselier757373e2016-02-18 00:20:34 +0000642public:
Howard Hinnant3e519522010-05-11 19:42:16 +0000643 typedef __hash_local_iterator<__non_const_node_pointer>
644 __non_const_iterator;
Eric Fiselier757373e2016-02-18 00:20:34 +0000645
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000646 typedef forward_iterator_tag iterator_category;
647 typedef typename _NodeTypes::__node_value_type value_type;
648 typedef typename _NodeTypes::difference_type difference_type;
649 typedef const value_type& reference;
650 typedef typename _NodeTypes::__const_node_value_type_pointer pointer;
Eric Fiselier934b0922015-12-30 21:52:00 +0000651
Howard Hinnant3e519522010-05-11 19:42:16 +0000652
Eric Fiselier40492ba2016-07-23 20:36:55 +0000653 _LIBCPP_INLINE_VISIBILITY __hash_const_local_iterator() _NOEXCEPT : __node_(nullptr) {
654 _LIBCPP_DEBUG_MODE(__get_db()->__insert_i(this));
Howard Hinnantb24c8022013-07-23 22:01:58 +0000655 }
656
Howard Hinnant43d99232010-09-21 17:32:39 +0000657 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +0000658 __hash_const_local_iterator(const __non_const_iterator& __x) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000659 : __node_(__x.__node_),
660 __bucket_(__x.__bucket_),
661 __bucket_count_(__x.__bucket_count_)
Howard Hinnantb24c8022013-07-23 22:01:58 +0000662 {
Eric Fiselier40492ba2016-07-23 20:36:55 +0000663 _LIBCPP_DEBUG_MODE(__get_db()->__iterator_copy(this, &__x));
Howard Hinnantb24c8022013-07-23 22:01:58 +0000664 }
665
666#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant43d99232010-09-21 17:32:39 +0000667 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb24c8022013-07-23 22:01:58 +0000668 __hash_const_local_iterator(const __hash_const_local_iterator& __i)
669 : __node_(__i.__node_),
670 __bucket_(__i.__bucket_),
671 __bucket_count_(__i.__bucket_count_)
672 {
673 __get_db()->__iterator_copy(this, &__i);
674 }
675
Howard Hinnant43d99232010-09-21 17:32:39 +0000676 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb24c8022013-07-23 22:01:58 +0000677 ~__hash_const_local_iterator()
678 {
679 __get_db()->__erase_i(this);
680 }
681
682 _LIBCPP_INLINE_VISIBILITY
683 __hash_const_local_iterator& operator=(const __hash_const_local_iterator& __i)
684 {
685 if (this != &__i)
686 {
687 __get_db()->__iterator_copy(this, &__i);
688 __node_ = __i.__node_;
689 __bucket_ = __i.__bucket_;
690 __bucket_count_ = __i.__bucket_count_;
691 }
692 return *this;
693 }
Howard Hinnantb24c8022013-07-23 22:01:58 +0000694#endif // _LIBCPP_DEBUG_LEVEL >= 2
695
696 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000697 reference operator*() const {
698 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
Howard Hinnantb24c8022013-07-23 22:01:58 +0000699 "Attempted to dereference a non-dereferenceable unordered container const_local_iterator");
Eric Fiselier40492ba2016-07-23 20:36:55 +0000700 return __node_->__upcast()->__value_;
701 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000702
Howard Hinnant43d99232010-09-21 17:32:39 +0000703 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000704 pointer operator->() const {
705 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
706 "Attempted to dereference a non-dereferenceable unordered container const_local_iterator");
707 return pointer_traits<pointer>::pointer_to(__node_->__upcast()->__value_);
708 }
709
710 _LIBCPP_INLINE_VISIBILITY
711 __hash_const_local_iterator& operator++() {
712 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
Howard Hinnantb24c8022013-07-23 22:01:58 +0000713 "Attempted to increment non-incrementable unordered container const_local_iterator");
Howard Hinnant3e519522010-05-11 19:42:16 +0000714 __node_ = __node_->__next_;
Eric Fiselier40492ba2016-07-23 20:36:55 +0000715 if (__node_ != nullptr && __constrain_hash(__node_->__hash(), __bucket_count_) != __bucket_)
Howard Hinnant3e519522010-05-11 19:42:16 +0000716 __node_ = nullptr;
717 return *this;
718 }
719
Howard Hinnant43d99232010-09-21 17:32:39 +0000720 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000721 __hash_const_local_iterator operator++(int)
722 {
723 __hash_const_local_iterator __t(*this);
724 ++(*this);
725 return __t;
726 }
727
Howard Hinnant43d99232010-09-21 17:32:39 +0000728 friend _LIBCPP_INLINE_VISIBILITY
729 bool operator==(const __hash_const_local_iterator& __x, const __hash_const_local_iterator& __y)
Howard Hinnantb24c8022013-07-23 22:01:58 +0000730 {
Howard Hinnantb24c8022013-07-23 22:01:58 +0000731 return __x.__node_ == __y.__node_;
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 {return !(__x == __y);}
Howard Hinnant3e519522010-05-11 19:42:16 +0000736
737private:
Howard Hinnantb24c8022013-07-23 22:01:58 +0000738#if _LIBCPP_DEBUG_LEVEL >= 2
739 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000740 __hash_const_local_iterator(__next_pointer __node, size_t __bucket,
Howard Hinnantb24c8022013-07-23 22:01:58 +0000741 size_t __bucket_count, const void* __c) _NOEXCEPT
742 : __node_(__node),
743 __bucket_(__bucket),
744 __bucket_count_(__bucket_count)
745 {
746 __get_db()->__insert_ic(this, __c);
747 if (__node_ != nullptr)
748 __node_ = __node_->__next_;
749 }
750#else
Howard Hinnant43d99232010-09-21 17:32:39 +0000751 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000752 __hash_const_local_iterator(__next_pointer __node, size_t __bucket,
Howard Hinnant37141072011-06-04 18:54:24 +0000753 size_t __bucket_count) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000754 : __node_(__node),
755 __bucket_(__bucket),
756 __bucket_count_(__bucket_count)
757 {
758 if (__node_ != nullptr)
759 __node_ = __node_->__next_;
760 }
Howard Hinnantb24c8022013-07-23 22:01:58 +0000761#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000762 template <class, class, class, class> friend class __hash_table;
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +0000763 template <class> friend class _LIBCPP_TEMPLATE_VIS __hash_map_const_iterator;
Howard Hinnant3e519522010-05-11 19:42:16 +0000764};
765
766template <class _Alloc>
767class __bucket_list_deallocator
768{
769 typedef _Alloc allocator_type;
770 typedef allocator_traits<allocator_type> __alloc_traits;
771 typedef typename __alloc_traits::size_type size_type;
772
773 __compressed_pair<size_type, allocator_type> __data_;
774public:
775 typedef typename __alloc_traits::pointer pointer;
776
Howard Hinnant43d99232010-09-21 17:32:39 +0000777 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000778 __bucket_list_deallocator()
Howard Hinnant37141072011-06-04 18:54:24 +0000779 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +0000780 : __data_(0) {}
Howard Hinnant43d99232010-09-21 17:32:39 +0000781
782 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000783 __bucket_list_deallocator(const allocator_type& __a, size_type __size)
Howard Hinnant37141072011-06-04 18:54:24 +0000784 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +0000785 : __data_(__size, __a) {}
786
Eric Fiselierafa7a952017-04-19 01:23:04 +0000787#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant43d99232010-09-21 17:32:39 +0000788 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000789 __bucket_list_deallocator(__bucket_list_deallocator&& __x)
Howard Hinnant37141072011-06-04 18:54:24 +0000790 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnantce48a112011-06-30 21:18:19 +0000791 : __data_(_VSTD::move(__x.__data_))
Howard Hinnant3e519522010-05-11 19:42:16 +0000792 {
793 __x.size() = 0;
794 }
Eric Fiselierafa7a952017-04-19 01:23:04 +0000795#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000796
Howard Hinnant37141072011-06-04 18:54:24 +0000797 _LIBCPP_INLINE_VISIBILITY
798 size_type& size() _NOEXCEPT {return __data_.first();}
799 _LIBCPP_INLINE_VISIBILITY
800 size_type size() const _NOEXCEPT {return __data_.first();}
Howard Hinnant3e519522010-05-11 19:42:16 +0000801
Howard Hinnant43d99232010-09-21 17:32:39 +0000802 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +0000803 allocator_type& __alloc() _NOEXCEPT {return __data_.second();}
804 _LIBCPP_INLINE_VISIBILITY
805 const allocator_type& __alloc() const _NOEXCEPT {return __data_.second();}
806
807 _LIBCPP_INLINE_VISIBILITY
808 void operator()(pointer __p) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000809 {
810 __alloc_traits::deallocate(__alloc(), __p, size());
811 }
812};
813
Howard Hinnantce534202011-06-14 19:58:17 +0000814template <class _Alloc> class __hash_map_node_destructor;
Howard Hinnant3e519522010-05-11 19:42:16 +0000815
816template <class _Alloc>
817class __hash_node_destructor
818{
819 typedef _Alloc allocator_type;
820 typedef allocator_traits<allocator_type> __alloc_traits;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000821
Howard Hinnant3e519522010-05-11 19:42:16 +0000822public:
823 typedef typename __alloc_traits::pointer pointer;
824private:
Eric Fiselierfcd02212016-02-11 11:59:44 +0000825 typedef __hash_node_types<pointer> _NodeTypes;
Howard Hinnant3e519522010-05-11 19:42:16 +0000826
827 allocator_type& __na_;
828
829 __hash_node_destructor& operator=(const __hash_node_destructor&);
830
831public:
832 bool __value_constructed;
833
Howard Hinnant43d99232010-09-21 17:32:39 +0000834 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf622b582011-07-31 17:04:30 +0000835 explicit __hash_node_destructor(allocator_type& __na,
836 bool __constructed = false) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000837 : __na_(__na),
Howard Hinnantf622b582011-07-31 17:04:30 +0000838 __value_constructed(__constructed)
Howard Hinnant3e519522010-05-11 19:42:16 +0000839 {}
840
Howard Hinnant43d99232010-09-21 17:32:39 +0000841 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +0000842 void operator()(pointer __p) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000843 {
844 if (__value_constructed)
Eric Fiselierfcd02212016-02-11 11:59:44 +0000845 __alloc_traits::destroy(__na_, _NodeTypes::__get_ptr(__p->__value_));
Howard Hinnant3e519522010-05-11 19:42:16 +0000846 if (__p)
847 __alloc_traits::deallocate(__na_, __p, 1);
848 }
849
850 template <class> friend class __hash_map_node_destructor;
851};
852
Erik Pilkingtonb0386a52018-08-01 01:33:38 +0000853#if _LIBCPP_STD_VER > 14
854template <class _NodeType, class _Alloc>
855struct __generic_container_node_destructor;
856
857template <class _Tp, class _VoidPtr, class _Alloc>
858struct __generic_container_node_destructor<__hash_node<_Tp, _VoidPtr>, _Alloc>
859 : __hash_node_destructor<_Alloc>
860{
861 using __hash_node_destructor<_Alloc>::__hash_node_destructor;
862};
863#endif
Eric Fiselier04333f92017-01-13 22:42:53 +0000864
Louis Dionne3560fbf32018-12-06 21:46:17 +0000865template <class _Key, class _Hash, class _Equal>
866struct __enforce_unordered_container_requirements {
Eric Fiselier04333f92017-01-13 22:42:53 +0000867#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierbd6a2d82017-03-03 22:35:58 +0000868 static_assert(__check_hash_requirements<_Key, _Hash>::value,
Louis Dionne3560fbf32018-12-06 21:46:17 +0000869 "the specified hash does not meet the Hash requirements");
Eric Fiselieracb21582017-03-01 02:02:28 +0000870 static_assert(is_copy_constructible<_Equal>::value,
Louis Dionne3560fbf32018-12-06 21:46:17 +0000871 "the specified comparator is required to be copy constructible");
872#endif
873 typedef int type;
Eric Fiselier04333f92017-01-13 22:42:53 +0000874};
875
Louis Dionne3560fbf32018-12-06 21:46:17 +0000876template <class _Key, class _Hash, class _Equal>
877#ifndef _LIBCPP_CXX03_LANG
878 _LIBCPP_DIAGNOSE_WARNING(!__invokable<_Equal const&, _Key const&, _Key const&>::value,
879 "the specified comparator type does not provide a const call operator")
880 _LIBCPP_DIAGNOSE_WARNING(!__invokable<_Hash const&, _Key const&>::value,
881 "the specified hash functor does not provide a const call operator")
882#endif
883typename __enforce_unordered_container_requirements<_Key, _Hash, _Equal>::type
884__diagnose_unordered_container_requirements(int);
885
886// This dummy overload is used so that the compiler won't emit a spurious
887// "no matching function for call to __diagnose_unordered_xxx" diagnostic
888// when the overload above causes a hard error.
889template <class _Key, class _Hash, class _Equal>
890int __diagnose_unordered_container_requirements(void*);
Eric Fiselier04333f92017-01-13 22:42:53 +0000891
Howard Hinnant3e519522010-05-11 19:42:16 +0000892template <class _Tp, class _Hash, class _Equal, class _Alloc>
893class __hash_table
894{
895public:
896 typedef _Tp value_type;
897 typedef _Hash hasher;
898 typedef _Equal key_equal;
899 typedef _Alloc allocator_type;
900
901private:
902 typedef allocator_traits<allocator_type> __alloc_traits;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000903 typedef typename
904 __make_hash_node_types<value_type, typename __alloc_traits::void_pointer>::type
905 _NodeTypes;
Howard Hinnant3e519522010-05-11 19:42:16 +0000906public:
Eric Fiselierfcd02212016-02-11 11:59:44 +0000907
908 typedef typename _NodeTypes::__node_value_type __node_value_type;
909 typedef typename _NodeTypes::__container_value_type __container_value_type;
Duncan P. N. Exon Smithfde79b42016-03-17 20:45:20 +0000910 typedef typename _NodeTypes::key_type key_type;
Howard Hinnant3e519522010-05-11 19:42:16 +0000911 typedef value_type& reference;
912 typedef const value_type& const_reference;
913 typedef typename __alloc_traits::pointer pointer;
914 typedef typename __alloc_traits::const_pointer const_pointer;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000915#ifndef _LIBCPP_ABI_FIX_UNORDERED_CONTAINER_SIZE_TYPE
Howard Hinnant3e519522010-05-11 19:42:16 +0000916 typedef typename __alloc_traits::size_type size_type;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000917#else
918 typedef typename _NodeTypes::size_type size_type;
919#endif
920 typedef typename _NodeTypes::difference_type difference_type;
Howard Hinnant3e519522010-05-11 19:42:16 +0000921public:
922 // Create __node
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000923
924 typedef typename _NodeTypes::__node_type __node;
Marshall Clow1f508012015-04-07 05:21:38 +0000925 typedef typename __rebind_alloc_helper<__alloc_traits, __node>::type __node_allocator;
Howard Hinnant3e519522010-05-11 19:42:16 +0000926 typedef allocator_traits<__node_allocator> __node_traits;
Eric Fiselier8e397682016-02-11 15:22:37 +0000927 typedef typename _NodeTypes::__void_pointer __void_pointer;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000928 typedef typename _NodeTypes::__node_pointer __node_pointer;
929 typedef typename _NodeTypes::__node_pointer __node_const_pointer;
930 typedef typename _NodeTypes::__node_base_type __first_node;
931 typedef typename _NodeTypes::__node_base_pointer __node_base_pointer;
Eric Fiselier40492ba2016-07-23 20:36:55 +0000932 typedef typename _NodeTypes::__next_pointer __next_pointer;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000933
934private:
935 // check for sane allocator pointer rebinding semantics. Rebinding the
936 // allocator for a new pointer type should be exactly the same as rebinding
937 // the pointer using 'pointer_traits'.
938 static_assert((is_same<__node_pointer, typename __node_traits::pointer>::value),
939 "Allocator does not rebind pointers in a sane manner.");
940 typedef typename __rebind_alloc_helper<__node_traits, __first_node>::type
941 __node_base_allocator;
942 typedef allocator_traits<__node_base_allocator> __node_base_traits;
943 static_assert((is_same<__node_base_pointer, typename __node_base_traits::pointer>::value),
944 "Allocator does not rebind pointers in a sane manner.");
Howard Hinnant3e519522010-05-11 19:42:16 +0000945
946private:
947
Eric Fiselier40492ba2016-07-23 20:36:55 +0000948 typedef typename __rebind_alloc_helper<__node_traits, __next_pointer>::type __pointer_allocator;
Howard Hinnant3e519522010-05-11 19:42:16 +0000949 typedef __bucket_list_deallocator<__pointer_allocator> __bucket_list_deleter;
Eric Fiselier40492ba2016-07-23 20:36:55 +0000950 typedef unique_ptr<__next_pointer[], __bucket_list_deleter> __bucket_list;
Howard Hinnant3e519522010-05-11 19:42:16 +0000951 typedef allocator_traits<__pointer_allocator> __pointer_alloc_traits;
Eric Fiselier40492ba2016-07-23 20:36:55 +0000952 typedef typename __bucket_list_deleter::pointer __node_pointer_pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000953
954 // --- Member data begin ---
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000955 __bucket_list __bucket_list_;
956 __compressed_pair<__first_node, __node_allocator> __p1_;
957 __compressed_pair<size_type, hasher> __p2_;
958 __compressed_pair<float, key_equal> __p3_;
Howard Hinnant3e519522010-05-11 19:42:16 +0000959 // --- Member data end ---
960
Howard Hinnant37141072011-06-04 18:54:24 +0000961 _LIBCPP_INLINE_VISIBILITY
962 size_type& size() _NOEXCEPT {return __p2_.first();}
Howard Hinnant3e519522010-05-11 19:42:16 +0000963public:
Howard Hinnant37141072011-06-04 18:54:24 +0000964 _LIBCPP_INLINE_VISIBILITY
965 size_type size() const _NOEXCEPT {return __p2_.first();}
Howard Hinnant3e519522010-05-11 19:42:16 +0000966
Howard Hinnant37141072011-06-04 18:54:24 +0000967 _LIBCPP_INLINE_VISIBILITY
968 hasher& hash_function() _NOEXCEPT {return __p2_.second();}
969 _LIBCPP_INLINE_VISIBILITY
970 const hasher& hash_function() const _NOEXCEPT {return __p2_.second();}
Howard Hinnant3e519522010-05-11 19:42:16 +0000971
Howard Hinnant37141072011-06-04 18:54:24 +0000972 _LIBCPP_INLINE_VISIBILITY
973 float& max_load_factor() _NOEXCEPT {return __p3_.first();}
974 _LIBCPP_INLINE_VISIBILITY
975 float max_load_factor() const _NOEXCEPT {return __p3_.first();}
Howard Hinnant3e519522010-05-11 19:42:16 +0000976
Howard Hinnant37141072011-06-04 18:54:24 +0000977 _LIBCPP_INLINE_VISIBILITY
978 key_equal& key_eq() _NOEXCEPT {return __p3_.second();}
979 _LIBCPP_INLINE_VISIBILITY
980 const key_equal& key_eq() const _NOEXCEPT {return __p3_.second();}
Howard Hinnant3e519522010-05-11 19:42:16 +0000981
Howard Hinnant37141072011-06-04 18:54:24 +0000982 _LIBCPP_INLINE_VISIBILITY
983 __node_allocator& __node_alloc() _NOEXCEPT {return __p1_.second();}
984 _LIBCPP_INLINE_VISIBILITY
985 const __node_allocator& __node_alloc() const _NOEXCEPT
986 {return __p1_.second();}
Howard Hinnant3e519522010-05-11 19:42:16 +0000987
988public:
989 typedef __hash_iterator<__node_pointer> iterator;
Howard Hinnant307f8142013-06-22 15:21:29 +0000990 typedef __hash_const_iterator<__node_pointer> const_iterator;
Howard Hinnant3e519522010-05-11 19:42:16 +0000991 typedef __hash_local_iterator<__node_pointer> local_iterator;
Howard Hinnant307f8142013-06-22 15:21:29 +0000992 typedef __hash_const_local_iterator<__node_pointer> const_local_iterator;
Howard Hinnant3e519522010-05-11 19:42:16 +0000993
Evgeniy Stepanov906c8722015-11-07 01:22:13 +0000994 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +0000995 __hash_table()
996 _NOEXCEPT_(
997 is_nothrow_default_constructible<__bucket_list>::value &&
998 is_nothrow_default_constructible<__first_node>::value &&
999 is_nothrow_default_constructible<__node_allocator>::value &&
1000 is_nothrow_default_constructible<hasher>::value &&
1001 is_nothrow_default_constructible<key_equal>::value);
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001002 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001003 __hash_table(const hasher& __hf, const key_equal& __eql);
1004 __hash_table(const hasher& __hf, const key_equal& __eql,
1005 const allocator_type& __a);
1006 explicit __hash_table(const allocator_type& __a);
1007 __hash_table(const __hash_table& __u);
1008 __hash_table(const __hash_table& __u, const allocator_type& __a);
Eric Fiselierfcd02212016-02-11 11:59:44 +00001009#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant37141072011-06-04 18:54:24 +00001010 __hash_table(__hash_table&& __u)
1011 _NOEXCEPT_(
1012 is_nothrow_move_constructible<__bucket_list>::value &&
1013 is_nothrow_move_constructible<__first_node>::value &&
1014 is_nothrow_move_constructible<__node_allocator>::value &&
1015 is_nothrow_move_constructible<hasher>::value &&
1016 is_nothrow_move_constructible<key_equal>::value);
Howard Hinnant3e519522010-05-11 19:42:16 +00001017 __hash_table(__hash_table&& __u, const allocator_type& __a);
Eric Fiselierfcd02212016-02-11 11:59:44 +00001018#endif // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001019 ~__hash_table();
1020
1021 __hash_table& operator=(const __hash_table& __u);
Eric Fiselierfcd02212016-02-11 11:59:44 +00001022#ifndef _LIBCPP_CXX03_LANG
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001023 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001024 __hash_table& operator=(__hash_table&& __u)
1025 _NOEXCEPT_(
1026 __node_traits::propagate_on_container_move_assignment::value &&
1027 is_nothrow_move_assignable<__node_allocator>::value &&
1028 is_nothrow_move_assignable<hasher>::value &&
1029 is_nothrow_move_assignable<key_equal>::value);
Howard Hinnant3e519522010-05-11 19:42:16 +00001030#endif
1031 template <class _InputIterator>
1032 void __assign_unique(_InputIterator __first, _InputIterator __last);
1033 template <class _InputIterator>
1034 void __assign_multi(_InputIterator __first, _InputIterator __last);
1035
Howard Hinnant43d99232010-09-21 17:32:39 +00001036 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001037 size_type max_size() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001038 {
Eric Fiselier55b31b4e2016-11-23 01:18:56 +00001039 return std::min<size_type>(
Eric Fiselier341c9dd2016-11-23 09:16:12 +00001040 __node_traits::max_size(__node_alloc()),
Eric Fiselier55b31b4e2016-11-23 01:18:56 +00001041 numeric_limits<difference_type >::max()
1042 );
Howard Hinnant3e519522010-05-11 19:42:16 +00001043 }
1044
Erik Pilkington5c4e07a2018-10-31 17:31:35 +00001045private:
1046 _LIBCPP_INLINE_VISIBILITY
1047 __next_pointer __node_insert_multi_prepare(size_t __cp_hash,
1048 value_type& __cp_val);
1049 _LIBCPP_INLINE_VISIBILITY
1050 void __node_insert_multi_perform(__node_pointer __cp,
1051 __next_pointer __pn) _NOEXCEPT;
1052
1053 _LIBCPP_INLINE_VISIBILITY
1054 __next_pointer __node_insert_unique_prepare(size_t __nd_hash,
1055 value_type& __nd_val);
1056 _LIBCPP_INLINE_VISIBILITY
1057 void __node_insert_unique_perform(__node_pointer __ptr) _NOEXCEPT;
1058
1059public:
1060 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001061 pair<iterator, bool> __node_insert_unique(__node_pointer __nd);
Erik Pilkington5c4e07a2018-10-31 17:31:35 +00001062 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001063 iterator __node_insert_multi(__node_pointer __nd);
Erik Pilkington5c4e07a2018-10-31 17:31:35 +00001064 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001065 iterator __node_insert_multi(const_iterator __p,
1066 __node_pointer __nd);
1067
Eric Fiselierfcd02212016-02-11 11:59:44 +00001068#ifndef _LIBCPP_CXX03_LANG
1069 template <class _Key, class ..._Args>
Duncan P. N. Exon Smithfde79b42016-03-17 20:45:20 +00001070 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierfcd02212016-02-11 11:59:44 +00001071 pair<iterator, bool> __emplace_unique_key_args(_Key const& __k, _Args&&... __args);
Howard Hinnant3e519522010-05-11 19:42:16 +00001072
Eric Fiselierfcd02212016-02-11 11:59:44 +00001073 template <class... _Args>
Duncan P. N. Exon Smithfde79b42016-03-17 20:45:20 +00001074 _LIBCPP_INLINE_VISIBILITY
1075 pair<iterator, bool> __emplace_unique_impl(_Args&&... __args);
1076
1077 template <class _Pp>
1078 _LIBCPP_INLINE_VISIBILITY
1079 pair<iterator, bool> __emplace_unique(_Pp&& __x) {
1080 return __emplace_unique_extract_key(_VSTD::forward<_Pp>(__x),
1081 __can_extract_key<_Pp, key_type>());
1082 }
Eric Fiselier50088682016-04-16 00:23:12 +00001083
1084 template <class _First, class _Second>
1085 _LIBCPP_INLINE_VISIBILITY
1086 typename enable_if<
1087 __can_extract_map_key<_First, key_type, __container_value_type>::value,
1088 pair<iterator, bool>
1089 >::type __emplace_unique(_First&& __f, _Second&& __s) {
1090 return __emplace_unique_key_args(__f, _VSTD::forward<_First>(__f),
1091 _VSTD::forward<_Second>(__s));
1092 }
1093
Eric Fiselierfcd02212016-02-11 11:59:44 +00001094 template <class... _Args>
Duncan P. N. Exon Smithfde79b42016-03-17 20:45:20 +00001095 _LIBCPP_INLINE_VISIBILITY
1096 pair<iterator, bool> __emplace_unique(_Args&&... __args) {
1097 return __emplace_unique_impl(_VSTD::forward<_Args>(__args)...);
1098 }
1099
1100 template <class _Pp>
1101 _LIBCPP_INLINE_VISIBILITY
1102 pair<iterator, bool>
1103 __emplace_unique_extract_key(_Pp&& __x, __extract_key_fail_tag) {
1104 return __emplace_unique_impl(_VSTD::forward<_Pp>(__x));
1105 }
1106 template <class _Pp>
1107 _LIBCPP_INLINE_VISIBILITY
1108 pair<iterator, bool>
1109 __emplace_unique_extract_key(_Pp&& __x, __extract_key_self_tag) {
1110 return __emplace_unique_key_args(__x, _VSTD::forward<_Pp>(__x));
1111 }
1112 template <class _Pp>
1113 _LIBCPP_INLINE_VISIBILITY
1114 pair<iterator, bool>
1115 __emplace_unique_extract_key(_Pp&& __x, __extract_key_first_tag) {
1116 return __emplace_unique_key_args(__x.first, _VSTD::forward<_Pp>(__x));
1117 }
1118
1119 template <class... _Args>
1120 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierfcd02212016-02-11 11:59:44 +00001121 iterator __emplace_multi(_Args&&... __args);
1122 template <class... _Args>
Duncan P. N. Exon Smithfde79b42016-03-17 20:45:20 +00001123 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierfcd02212016-02-11 11:59:44 +00001124 iterator __emplace_hint_multi(const_iterator __p, _Args&&... __args);
1125
1126
Eric Fiselierde3f2b32015-06-13 07:18:32 +00001127 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierfcd02212016-02-11 11:59:44 +00001128 pair<iterator, bool>
1129 __insert_unique(__container_value_type&& __x) {
1130 return __emplace_unique_key_args(_NodeTypes::__get_key(__x), _VSTD::move(__x));
1131 }
1132
1133 template <class _Pp, class = typename enable_if<
1134 !__is_same_uncvref<_Pp, __container_value_type>::value
1135 >::type>
Eric Fiselierde3f2b32015-06-13 07:18:32 +00001136 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierfcd02212016-02-11 11:59:44 +00001137 pair<iterator, bool> __insert_unique(_Pp&& __x) {
1138 return __emplace_unique(_VSTD::forward<_Pp>(__x));
1139 }
1140
1141 template <class _Pp>
1142 _LIBCPP_INLINE_VISIBILITY
1143 iterator __insert_multi(_Pp&& __x) {
1144 return __emplace_multi(_VSTD::forward<_Pp>(__x));
1145 }
1146
1147 template <class _Pp>
1148 _LIBCPP_INLINE_VISIBILITY
1149 iterator __insert_multi(const_iterator __p, _Pp&& __x) {
1150 return __emplace_hint_multi(__p, _VSTD::forward<_Pp>(__x));
1151 }
1152
1153#else // !defined(_LIBCPP_CXX03_LANG)
1154 template <class _Key, class _Args>
Eric Fiselier4271d012016-09-25 04:05:46 +00001155 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierfcd02212016-02-11 11:59:44 +00001156 pair<iterator, bool> __emplace_unique_key_args(_Key const&, _Args& __args);
1157
1158 iterator __insert_multi(const __container_value_type& __x);
1159 iterator __insert_multi(const_iterator __p, const __container_value_type& __x);
Eric Fiselierde3f2b32015-06-13 07:18:32 +00001160#endif
1161
Eric Fiselierfcd02212016-02-11 11:59:44 +00001162 _LIBCPP_INLINE_VISIBILITY
1163 pair<iterator, bool> __insert_unique(const __container_value_type& __x) {
1164 return __emplace_unique_key_args(_NodeTypes::__get_key(__x), __x);
1165 }
Howard Hinnant3e519522010-05-11 19:42:16 +00001166
Erik Pilkingtonb0386a52018-08-01 01:33:38 +00001167#if _LIBCPP_STD_VER > 14
1168 template <class _NodeHandle, class _InsertReturnType>
1169 _LIBCPP_INLINE_VISIBILITY
1170 _InsertReturnType __node_handle_insert_unique(_NodeHandle&& __nh);
1171 template <class _NodeHandle>
1172 _LIBCPP_INLINE_VISIBILITY
1173 iterator __node_handle_insert_unique(const_iterator __hint,
1174 _NodeHandle&& __nh);
Erik Pilkington5c4e07a2018-10-31 17:31:35 +00001175 template <class _Table>
1176 _LIBCPP_INLINE_VISIBILITY
1177 void __node_handle_merge_unique(_Table& __source);
Erik Pilkingtonb0386a52018-08-01 01:33:38 +00001178
1179 template <class _NodeHandle>
1180 _LIBCPP_INLINE_VISIBILITY
1181 iterator __node_handle_insert_multi(_NodeHandle&& __nh);
1182 template <class _NodeHandle>
1183 _LIBCPP_INLINE_VISIBILITY
1184 iterator __node_handle_insert_multi(const_iterator __hint, _NodeHandle&& __nh);
Erik Pilkington5c4e07a2018-10-31 17:31:35 +00001185 template <class _Table>
1186 _LIBCPP_INLINE_VISIBILITY
1187 void __node_handle_merge_multi(_Table& __source);
Erik Pilkingtonb0386a52018-08-01 01:33:38 +00001188
1189 template <class _NodeHandle>
1190 _LIBCPP_INLINE_VISIBILITY
1191 _NodeHandle __node_handle_extract(key_type const& __key);
1192 template <class _NodeHandle>
1193 _LIBCPP_INLINE_VISIBILITY
1194 _NodeHandle __node_handle_extract(const_iterator __it);
1195#endif
1196
Howard Hinnant37141072011-06-04 18:54:24 +00001197 void clear() _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +00001198 void rehash(size_type __n);
Howard Hinnant43d99232010-09-21 17:32:39 +00001199 _LIBCPP_INLINE_VISIBILITY void reserve(size_type __n)
Howard Hinnant3e519522010-05-11 19:42:16 +00001200 {rehash(static_cast<size_type>(ceil(__n / max_load_factor())));}
Howard Hinnant43d99232010-09-21 17:32:39 +00001201
1202 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001203 size_type bucket_count() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001204 {
1205 return __bucket_list_.get_deleter().size();
1206 }
1207
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001208 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001209 iterator begin() _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001210 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001211 iterator end() _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001212 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001213 const_iterator begin() const _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001214 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001215 const_iterator end() const _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +00001216
1217 template <class _Key>
Howard Hinnant43d99232010-09-21 17:32:39 +00001218 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001219 size_type bucket(const _Key& __k) const
Howard Hinnante5c13de2013-07-29 19:05:47 +00001220 {
1221 _LIBCPP_ASSERT(bucket_count() > 0,
1222 "unordered container::bucket(key) called when bucket_count() == 0");
1223 return __constrain_hash(hash_function()(__k), bucket_count());
1224 }
Howard Hinnant3e519522010-05-11 19:42:16 +00001225
1226 template <class _Key>
1227 iterator find(const _Key& __x);
1228 template <class _Key>
1229 const_iterator find(const _Key& __x) const;
1230
Howard Hinnantc003db12011-11-29 18:15:50 +00001231 typedef __hash_node_destructor<__node_allocator> _Dp;
1232 typedef unique_ptr<__node, _Dp> __node_holder;
Howard Hinnant3e519522010-05-11 19:42:16 +00001233
1234 iterator erase(const_iterator __p);
1235 iterator erase(const_iterator __first, const_iterator __last);
1236 template <class _Key>
1237 size_type __erase_unique(const _Key& __k);
1238 template <class _Key>
1239 size_type __erase_multi(const _Key& __k);
Howard Hinnant37141072011-06-04 18:54:24 +00001240 __node_holder remove(const_iterator __p) _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +00001241
1242 template <class _Key>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001243 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001244 size_type __count_unique(const _Key& __k) const;
1245 template <class _Key>
1246 size_type __count_multi(const _Key& __k) const;
1247
1248 template <class _Key>
1249 pair<iterator, iterator>
1250 __equal_range_unique(const _Key& __k);
1251 template <class _Key>
1252 pair<const_iterator, const_iterator>
1253 __equal_range_unique(const _Key& __k) const;
1254
1255 template <class _Key>
1256 pair<iterator, iterator>
1257 __equal_range_multi(const _Key& __k);
1258 template <class _Key>
1259 pair<const_iterator, const_iterator>
1260 __equal_range_multi(const _Key& __k) const;
1261
Howard Hinnant37141072011-06-04 18:54:24 +00001262 void swap(__hash_table& __u)
Eric Fiselier87a82492015-07-18 20:40:46 +00001263#if _LIBCPP_STD_VER <= 11
Eric Fiselier780b51d2016-12-28 05:53:01 +00001264 _NOEXCEPT_DEBUG_(
Marshall Clowe3fbe142015-07-13 20:04:56 +00001265 __is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value
Marshall Clowe3fbe142015-07-13 20:04:56 +00001266 && (!allocator_traits<__pointer_allocator>::propagate_on_container_swap::value
1267 || __is_nothrow_swappable<__pointer_allocator>::value)
1268 && (!__node_traits::propagate_on_container_swap::value
1269 || __is_nothrow_swappable<__node_allocator>::value)
Marshall Clowe3fbe142015-07-13 20:04:56 +00001270 );
Eric Fiselier87a82492015-07-18 20:40:46 +00001271#else
Eric Fiselier780b51d2016-12-28 05:53:01 +00001272 _NOEXCEPT_DEBUG_(__is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value);
Eric Fiselier87a82492015-07-18 20:40:46 +00001273#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001274
Howard Hinnant43d99232010-09-21 17:32:39 +00001275 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001276 size_type max_bucket_count() const _NOEXCEPT
Eric Fiselier55b31b4e2016-11-23 01:18:56 +00001277 {return max_size(); }
Howard Hinnant3e519522010-05-11 19:42:16 +00001278 size_type bucket_size(size_type __n) const;
Howard Hinnant37141072011-06-04 18:54:24 +00001279 _LIBCPP_INLINE_VISIBILITY float load_factor() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001280 {
1281 size_type __bc = bucket_count();
1282 return __bc != 0 ? (float)size() / __bc : 0.f;
1283 }
Howard Hinnant37141072011-06-04 18:54:24 +00001284 _LIBCPP_INLINE_VISIBILITY void max_load_factor(float __mlf) _NOEXCEPT
Howard Hinnante5c13de2013-07-29 19:05:47 +00001285 {
1286 _LIBCPP_ASSERT(__mlf > 0,
1287 "unordered container::max_load_factor(lf) called with lf <= 0");
1288 max_load_factor() = _VSTD::max(__mlf, load_factor());
1289 }
Howard Hinnant3e519522010-05-11 19:42:16 +00001290
Howard Hinnantb24c8022013-07-23 22:01:58 +00001291 _LIBCPP_INLINE_VISIBILITY
1292 local_iterator
1293 begin(size_type __n)
1294 {
Howard Hinnante5c13de2013-07-29 19:05:47 +00001295 _LIBCPP_ASSERT(__n < bucket_count(),
1296 "unordered container::begin(n) called with n >= bucket_count()");
Howard Hinnantb24c8022013-07-23 22:01:58 +00001297#if _LIBCPP_DEBUG_LEVEL >= 2
1298 return local_iterator(__bucket_list_[__n], __n, bucket_count(), this);
1299#else
1300 return local_iterator(__bucket_list_[__n], __n, bucket_count());
1301#endif
1302 }
1303
1304 _LIBCPP_INLINE_VISIBILITY
1305 local_iterator
1306 end(size_type __n)
1307 {
Howard Hinnante5c13de2013-07-29 19:05:47 +00001308 _LIBCPP_ASSERT(__n < bucket_count(),
1309 "unordered container::end(n) called with n >= bucket_count()");
Howard Hinnantb24c8022013-07-23 22:01:58 +00001310#if _LIBCPP_DEBUG_LEVEL >= 2
1311 return local_iterator(nullptr, __n, bucket_count(), this);
1312#else
1313 return local_iterator(nullptr, __n, bucket_count());
1314#endif
1315 }
1316
1317 _LIBCPP_INLINE_VISIBILITY
1318 const_local_iterator
1319 cbegin(size_type __n) const
1320 {
Howard Hinnante5c13de2013-07-29 19:05:47 +00001321 _LIBCPP_ASSERT(__n < bucket_count(),
1322 "unordered container::cbegin(n) called with n >= bucket_count()");
Howard Hinnantb24c8022013-07-23 22:01:58 +00001323#if _LIBCPP_DEBUG_LEVEL >= 2
1324 return const_local_iterator(__bucket_list_[__n], __n, bucket_count(), this);
1325#else
1326 return const_local_iterator(__bucket_list_[__n], __n, bucket_count());
1327#endif
1328 }
1329
1330 _LIBCPP_INLINE_VISIBILITY
1331 const_local_iterator
1332 cend(size_type __n) const
1333 {
Howard Hinnante5c13de2013-07-29 19:05:47 +00001334 _LIBCPP_ASSERT(__n < bucket_count(),
1335 "unordered container::cend(n) called with n >= bucket_count()");
Howard Hinnantb24c8022013-07-23 22:01:58 +00001336#if _LIBCPP_DEBUG_LEVEL >= 2
1337 return const_local_iterator(nullptr, __n, bucket_count(), this);
1338#else
1339 return const_local_iterator(nullptr, __n, bucket_count());
1340#endif
1341 }
1342
1343#if _LIBCPP_DEBUG_LEVEL >= 2
1344
1345 bool __dereferenceable(const const_iterator* __i) const;
1346 bool __decrementable(const const_iterator* __i) const;
1347 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1348 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1349
1350#endif // _LIBCPP_DEBUG_LEVEL >= 2
1351
Howard Hinnant3e519522010-05-11 19:42:16 +00001352private:
1353 void __rehash(size_type __n);
1354
Eric Fiselierfcd02212016-02-11 11:59:44 +00001355#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001356 template <class ..._Args>
Eric Fiselierfcd02212016-02-11 11:59:44 +00001357 __node_holder __construct_node(_Args&& ...__args);
1358
1359 template <class _First, class ..._Rest>
1360 __node_holder __construct_node_hash(size_t __hash, _First&& __f, _Rest&&... __rest);
1361#else // _LIBCPP_CXX03_LANG
1362 __node_holder __construct_node(const __container_value_type& __v);
1363 __node_holder __construct_node_hash(size_t __hash, const __container_value_type& __v);
Howard Hinnant3e519522010-05-11 19:42:16 +00001364#endif
Eric Fiselierfcd02212016-02-11 11:59:44 +00001365
Howard Hinnant3e519522010-05-11 19:42:16 +00001366
Howard Hinnant43d99232010-09-21 17:32:39 +00001367 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001368 void __copy_assign_alloc(const __hash_table& __u)
1369 {__copy_assign_alloc(__u, integral_constant<bool,
1370 __node_traits::propagate_on_container_copy_assignment::value>());}
1371 void __copy_assign_alloc(const __hash_table& __u, true_type);
Howard Hinnant43d99232010-09-21 17:32:39 +00001372 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2063662011-12-01 20:21:04 +00001373 void __copy_assign_alloc(const __hash_table&, false_type) {}
Howard Hinnant3e519522010-05-11 19:42:16 +00001374
Eric Fiselierfcd02212016-02-11 11:59:44 +00001375#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001376 void __move_assign(__hash_table& __u, false_type);
Howard Hinnant37141072011-06-04 18:54:24 +00001377 void __move_assign(__hash_table& __u, true_type)
1378 _NOEXCEPT_(
1379 is_nothrow_move_assignable<__node_allocator>::value &&
1380 is_nothrow_move_assignable<hasher>::value &&
1381 is_nothrow_move_assignable<key_equal>::value);
1382 _LIBCPP_INLINE_VISIBILITY
1383 void __move_assign_alloc(__hash_table& __u)
1384 _NOEXCEPT_(
1385 !__node_traits::propagate_on_container_move_assignment::value ||
1386 (is_nothrow_move_assignable<__pointer_allocator>::value &&
1387 is_nothrow_move_assignable<__node_allocator>::value))
Howard Hinnant3e519522010-05-11 19:42:16 +00001388 {__move_assign_alloc(__u, integral_constant<bool,
1389 __node_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnant43d99232010-09-21 17:32:39 +00001390 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001391 void __move_assign_alloc(__hash_table& __u, true_type)
Howard Hinnant37141072011-06-04 18:54:24 +00001392 _NOEXCEPT_(
1393 is_nothrow_move_assignable<__pointer_allocator>::value &&
1394 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +00001395 {
1396 __bucket_list_.get_deleter().__alloc() =
Howard Hinnantce48a112011-06-30 21:18:19 +00001397 _VSTD::move(__u.__bucket_list_.get_deleter().__alloc());
1398 __node_alloc() = _VSTD::move(__u.__node_alloc());
Howard Hinnant3e519522010-05-11 19:42:16 +00001399 }
Howard Hinnant43d99232010-09-21 17:32:39 +00001400 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001401 void __move_assign_alloc(__hash_table&, false_type) _NOEXCEPT {}
Eric Fiselierfcd02212016-02-11 11:59:44 +00001402#endif // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001403
Eric Fiseliercd71f442017-01-07 03:01:24 +00001404 void __deallocate_node(__next_pointer __np) _NOEXCEPT;
Eric Fiselier40492ba2016-07-23 20:36:55 +00001405 __next_pointer __detach() _NOEXCEPT;
Howard Hinnant307f8142013-06-22 15:21:29 +00001406
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +00001407 template <class, class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS unordered_map;
1408 template <class, class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS unordered_multimap;
Howard Hinnant3e519522010-05-11 19:42:16 +00001409};
1410
1411template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001412inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001413__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table()
Howard Hinnant37141072011-06-04 18:54:24 +00001414 _NOEXCEPT_(
1415 is_nothrow_default_constructible<__bucket_list>::value &&
1416 is_nothrow_default_constructible<__first_node>::value &&
Eric Fiseliere2e332a2015-12-16 00:53:04 +00001417 is_nothrow_default_constructible<__node_allocator>::value &&
Howard Hinnant37141072011-06-04 18:54:24 +00001418 is_nothrow_default_constructible<hasher>::value &&
1419 is_nothrow_default_constructible<key_equal>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +00001420 : __p2_(0),
1421 __p3_(1.0f)
1422{
1423}
1424
1425template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001426inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001427__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const hasher& __hf,
1428 const key_equal& __eql)
1429 : __bucket_list_(nullptr, __bucket_list_deleter()),
1430 __p1_(),
1431 __p2_(0, __hf),
1432 __p3_(1.0f, __eql)
1433{
1434}
1435
1436template <class _Tp, class _Hash, class _Equal, class _Alloc>
1437__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const hasher& __hf,
1438 const key_equal& __eql,
1439 const allocator_type& __a)
1440 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
Eric Fiselierc88580c2017-04-12 23:45:53 +00001441 __p1_(__second_tag(), __node_allocator(__a)),
Howard Hinnant3e519522010-05-11 19:42:16 +00001442 __p2_(0, __hf),
1443 __p3_(1.0f, __eql)
1444{
1445}
1446
1447template <class _Tp, class _Hash, class _Equal, class _Alloc>
1448__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const allocator_type& __a)
1449 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
Eric Fiselierc88580c2017-04-12 23:45:53 +00001450 __p1_(__second_tag(), __node_allocator(__a)),
Howard Hinnant3e519522010-05-11 19:42:16 +00001451 __p2_(0),
1452 __p3_(1.0f)
1453{
1454}
1455
1456template <class _Tp, class _Hash, class _Equal, class _Alloc>
1457__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const __hash_table& __u)
1458 : __bucket_list_(nullptr,
1459 __bucket_list_deleter(allocator_traits<__pointer_allocator>::
1460 select_on_container_copy_construction(
1461 __u.__bucket_list_.get_deleter().__alloc()), 0)),
Eric Fiselierc88580c2017-04-12 23:45:53 +00001462 __p1_(__second_tag(), allocator_traits<__node_allocator>::
Howard Hinnant3e519522010-05-11 19:42:16 +00001463 select_on_container_copy_construction(__u.__node_alloc())),
1464 __p2_(0, __u.hash_function()),
1465 __p3_(__u.__p3_)
1466{
1467}
1468
1469template <class _Tp, class _Hash, class _Equal, class _Alloc>
1470__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const __hash_table& __u,
1471 const allocator_type& __a)
1472 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
Eric Fiselierc88580c2017-04-12 23:45:53 +00001473 __p1_(__second_tag(), __node_allocator(__a)),
Howard Hinnant3e519522010-05-11 19:42:16 +00001474 __p2_(0, __u.hash_function()),
1475 __p3_(__u.__p3_)
1476{
1477}
1478
Eric Fiselierfcd02212016-02-11 11:59:44 +00001479#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001480
1481template <class _Tp, class _Hash, class _Equal, class _Alloc>
1482__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(__hash_table&& __u)
Howard Hinnant37141072011-06-04 18:54:24 +00001483 _NOEXCEPT_(
1484 is_nothrow_move_constructible<__bucket_list>::value &&
1485 is_nothrow_move_constructible<__first_node>::value &&
Eric Fiseliere2e332a2015-12-16 00:53:04 +00001486 is_nothrow_move_constructible<__node_allocator>::value &&
Howard Hinnant37141072011-06-04 18:54:24 +00001487 is_nothrow_move_constructible<hasher>::value &&
1488 is_nothrow_move_constructible<key_equal>::value)
Howard Hinnantce48a112011-06-30 21:18:19 +00001489 : __bucket_list_(_VSTD::move(__u.__bucket_list_)),
1490 __p1_(_VSTD::move(__u.__p1_)),
1491 __p2_(_VSTD::move(__u.__p2_)),
1492 __p3_(_VSTD::move(__u.__p3_))
Howard Hinnant3e519522010-05-11 19:42:16 +00001493{
1494 if (size() > 0)
1495 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001496 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash(), bucket_count())] =
1497 __p1_.first().__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001498 __u.__p1_.first().__next_ = nullptr;
1499 __u.size() = 0;
1500 }
1501}
1502
1503template <class _Tp, class _Hash, class _Equal, class _Alloc>
1504__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(__hash_table&& __u,
1505 const allocator_type& __a)
1506 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
Eric Fiselierc88580c2017-04-12 23:45:53 +00001507 __p1_(__second_tag(), __node_allocator(__a)),
Howard Hinnantce48a112011-06-30 21:18:19 +00001508 __p2_(0, _VSTD::move(__u.hash_function())),
1509 __p3_(_VSTD::move(__u.__p3_))
Howard Hinnant3e519522010-05-11 19:42:16 +00001510{
1511 if (__a == allocator_type(__u.__node_alloc()))
1512 {
1513 __bucket_list_.reset(__u.__bucket_list_.release());
1514 __bucket_list_.get_deleter().size() = __u.__bucket_list_.get_deleter().size();
1515 __u.__bucket_list_.get_deleter().size() = 0;
1516 if (__u.size() > 0)
1517 {
1518 __p1_.first().__next_ = __u.__p1_.first().__next_;
1519 __u.__p1_.first().__next_ = nullptr;
Eric Fiselier40492ba2016-07-23 20:36:55 +00001520 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash(), bucket_count())] =
1521 __p1_.first().__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001522 size() = __u.size();
1523 __u.size() = 0;
1524 }
1525 }
1526}
1527
Eric Fiselierfcd02212016-02-11 11:59:44 +00001528#endif // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001529
1530template <class _Tp, class _Hash, class _Equal, class _Alloc>
1531__hash_table<_Tp, _Hash, _Equal, _Alloc>::~__hash_table()
1532{
Eric Fiselieracb21582017-03-01 02:02:28 +00001533#if defined(_LIBCPP_CXX03_LANG)
Marshall Clow3b8669e2016-06-30 22:05:45 +00001534 static_assert((is_copy_constructible<key_equal>::value),
1535 "Predicate must be copy-constructible.");
1536 static_assert((is_copy_constructible<hasher>::value),
1537 "Hasher must be copy-constructible.");
Eric Fiselier04333f92017-01-13 22:42:53 +00001538#endif
Eric Fiselieracb21582017-03-01 02:02:28 +00001539
Eric Fiseliercd71f442017-01-07 03:01:24 +00001540 __deallocate_node(__p1_.first().__next_);
Howard Hinnantb24c8022013-07-23 22:01:58 +00001541#if _LIBCPP_DEBUG_LEVEL >= 2
1542 __get_db()->__erase_c(this);
1543#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001544}
1545
1546template <class _Tp, class _Hash, class _Equal, class _Alloc>
1547void
1548__hash_table<_Tp, _Hash, _Equal, _Alloc>::__copy_assign_alloc(
1549 const __hash_table& __u, true_type)
1550{
1551 if (__node_alloc() != __u.__node_alloc())
1552 {
1553 clear();
1554 __bucket_list_.reset();
1555 __bucket_list_.get_deleter().size() = 0;
1556 }
1557 __bucket_list_.get_deleter().__alloc() = __u.__bucket_list_.get_deleter().__alloc();
1558 __node_alloc() = __u.__node_alloc();
1559}
1560
1561template <class _Tp, class _Hash, class _Equal, class _Alloc>
1562__hash_table<_Tp, _Hash, _Equal, _Alloc>&
1563__hash_table<_Tp, _Hash, _Equal, _Alloc>::operator=(const __hash_table& __u)
1564{
1565 if (this != &__u)
1566 {
1567 __copy_assign_alloc(__u);
1568 hash_function() = __u.hash_function();
1569 key_eq() = __u.key_eq();
1570 max_load_factor() = __u.max_load_factor();
1571 __assign_multi(__u.begin(), __u.end());
1572 }
1573 return *this;
1574}
1575
1576template <class _Tp, class _Hash, class _Equal, class _Alloc>
1577void
Eric Fiseliercd71f442017-01-07 03:01:24 +00001578__hash_table<_Tp, _Hash, _Equal, _Alloc>::__deallocate_node(__next_pointer __np)
Howard Hinnant37141072011-06-04 18:54:24 +00001579 _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001580{
1581 __node_allocator& __na = __node_alloc();
1582 while (__np != nullptr)
1583 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001584 __next_pointer __next = __np->__next_;
Howard Hinnantb24c8022013-07-23 22:01:58 +00001585#if _LIBCPP_DEBUG_LEVEL >= 2
1586 __c_node* __c = __get_db()->__find_c_and_lock(this);
1587 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1588 {
1589 --__p;
1590 iterator* __i = static_cast<iterator*>((*__p)->__i_);
1591 if (__i->__node_ == __np)
1592 {
1593 (*__p)->__c_ = nullptr;
1594 if (--__c->end_ != __p)
1595 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1596 }
1597 }
1598 __get_db()->unlock();
1599#endif
Eric Fiselier40492ba2016-07-23 20:36:55 +00001600 __node_pointer __real_np = __np->__upcast();
1601 __node_traits::destroy(__na, _NodeTypes::__get_ptr(__real_np->__value_));
1602 __node_traits::deallocate(__na, __real_np, 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001603 __np = __next;
1604 }
1605}
1606
1607template <class _Tp, class _Hash, class _Equal, class _Alloc>
Eric Fiselier40492ba2016-07-23 20:36:55 +00001608typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__next_pointer
Howard Hinnant37141072011-06-04 18:54:24 +00001609__hash_table<_Tp, _Hash, _Equal, _Alloc>::__detach() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001610{
1611 size_type __bc = bucket_count();
1612 for (size_type __i = 0; __i < __bc; ++__i)
1613 __bucket_list_[__i] = nullptr;
1614 size() = 0;
Eric Fiselier40492ba2016-07-23 20:36:55 +00001615 __next_pointer __cache = __p1_.first().__next_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001616 __p1_.first().__next_ = nullptr;
1617 return __cache;
1618}
1619
Eric Fiselierfcd02212016-02-11 11:59:44 +00001620#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001621
1622template <class _Tp, class _Hash, class _Equal, class _Alloc>
1623void
1624__hash_table<_Tp, _Hash, _Equal, _Alloc>::__move_assign(
1625 __hash_table& __u, true_type)
Howard Hinnant37141072011-06-04 18:54:24 +00001626 _NOEXCEPT_(
1627 is_nothrow_move_assignable<__node_allocator>::value &&
1628 is_nothrow_move_assignable<hasher>::value &&
1629 is_nothrow_move_assignable<key_equal>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +00001630{
1631 clear();
1632 __bucket_list_.reset(__u.__bucket_list_.release());
1633 __bucket_list_.get_deleter().size() = __u.__bucket_list_.get_deleter().size();
1634 __u.__bucket_list_.get_deleter().size() = 0;
1635 __move_assign_alloc(__u);
1636 size() = __u.size();
Howard Hinnantce48a112011-06-30 21:18:19 +00001637 hash_function() = _VSTD::move(__u.hash_function());
Howard Hinnant3e519522010-05-11 19:42:16 +00001638 max_load_factor() = __u.max_load_factor();
Howard Hinnantce48a112011-06-30 21:18:19 +00001639 key_eq() = _VSTD::move(__u.key_eq());
Howard Hinnant3e519522010-05-11 19:42:16 +00001640 __p1_.first().__next_ = __u.__p1_.first().__next_;
1641 if (size() > 0)
1642 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001643 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash(), bucket_count())] =
1644 __p1_.first().__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001645 __u.__p1_.first().__next_ = nullptr;
1646 __u.size() = 0;
1647 }
Howard Hinnantb24c8022013-07-23 22:01:58 +00001648#if _LIBCPP_DEBUG_LEVEL >= 2
1649 __get_db()->swap(this, &__u);
1650#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001651}
1652
1653template <class _Tp, class _Hash, class _Equal, class _Alloc>
1654void
1655__hash_table<_Tp, _Hash, _Equal, _Alloc>::__move_assign(
1656 __hash_table& __u, false_type)
1657{
1658 if (__node_alloc() == __u.__node_alloc())
1659 __move_assign(__u, true_type());
1660 else
1661 {
Howard Hinnantce48a112011-06-30 21:18:19 +00001662 hash_function() = _VSTD::move(__u.hash_function());
1663 key_eq() = _VSTD::move(__u.key_eq());
Howard Hinnant3e519522010-05-11 19:42:16 +00001664 max_load_factor() = __u.max_load_factor();
1665 if (bucket_count() != 0)
1666 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001667 __next_pointer __cache = __detach();
Howard Hinnant3e519522010-05-11 19:42:16 +00001668#ifndef _LIBCPP_NO_EXCEPTIONS
1669 try
1670 {
Howard Hinnantb3371f62010-08-22 00:02:43 +00001671#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001672 const_iterator __i = __u.begin();
1673 while (__cache != nullptr && __u.size() != 0)
1674 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001675 __cache->__upcast()->__value_ =
1676 _VSTD::move(__u.remove(__i++)->__value_);
1677 __next_pointer __next = __cache->__next_;
1678 __node_insert_multi(__cache->__upcast());
Howard Hinnant3e519522010-05-11 19:42:16 +00001679 __cache = __next;
1680 }
1681#ifndef _LIBCPP_NO_EXCEPTIONS
1682 }
1683 catch (...)
1684 {
Eric Fiseliercd71f442017-01-07 03:01:24 +00001685 __deallocate_node(__cache);
Howard Hinnant3e519522010-05-11 19:42:16 +00001686 throw;
1687 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00001688#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiseliercd71f442017-01-07 03:01:24 +00001689 __deallocate_node(__cache);
Howard Hinnant3e519522010-05-11 19:42:16 +00001690 }
1691 const_iterator __i = __u.begin();
1692 while (__u.size() != 0)
1693 {
Eric Fiselierfcd02212016-02-11 11:59:44 +00001694 __node_holder __h = __construct_node(_NodeTypes::__move(__u.remove(__i++)->__value_));
Howard Hinnant3e519522010-05-11 19:42:16 +00001695 __node_insert_multi(__h.get());
1696 __h.release();
1697 }
1698 }
1699}
1700
1701template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001702inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001703__hash_table<_Tp, _Hash, _Equal, _Alloc>&
1704__hash_table<_Tp, _Hash, _Equal, _Alloc>::operator=(__hash_table&& __u)
Howard Hinnant37141072011-06-04 18:54:24 +00001705 _NOEXCEPT_(
1706 __node_traits::propagate_on_container_move_assignment::value &&
1707 is_nothrow_move_assignable<__node_allocator>::value &&
1708 is_nothrow_move_assignable<hasher>::value &&
1709 is_nothrow_move_assignable<key_equal>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +00001710{
1711 __move_assign(__u, integral_constant<bool,
1712 __node_traits::propagate_on_container_move_assignment::value>());
1713 return *this;
1714}
1715
Eric Fiselierfcd02212016-02-11 11:59:44 +00001716#endif // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001717
1718template <class _Tp, class _Hash, class _Equal, class _Alloc>
1719template <class _InputIterator>
1720void
1721__hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_unique(_InputIterator __first,
1722 _InputIterator __last)
1723{
Eric Fiselierfcd02212016-02-11 11:59:44 +00001724 typedef iterator_traits<_InputIterator> _ITraits;
1725 typedef typename _ITraits::value_type _ItValueType;
1726 static_assert((is_same<_ItValueType, __container_value_type>::value),
1727 "__assign_unique may only be called with the containers value type");
1728
Howard Hinnant3e519522010-05-11 19:42:16 +00001729 if (bucket_count() != 0)
1730 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001731 __next_pointer __cache = __detach();
Howard Hinnant3e519522010-05-11 19:42:16 +00001732#ifndef _LIBCPP_NO_EXCEPTIONS
1733 try
1734 {
Howard Hinnantb3371f62010-08-22 00:02:43 +00001735#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001736 for (; __cache != nullptr && __first != __last; ++__first)
1737 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001738 __cache->__upcast()->__value_ = *__first;
1739 __next_pointer __next = __cache->__next_;
1740 __node_insert_unique(__cache->__upcast());
Howard Hinnant3e519522010-05-11 19:42:16 +00001741 __cache = __next;
1742 }
1743#ifndef _LIBCPP_NO_EXCEPTIONS
1744 }
1745 catch (...)
1746 {
Eric Fiseliercd71f442017-01-07 03:01:24 +00001747 __deallocate_node(__cache);
Howard Hinnant3e519522010-05-11 19:42:16 +00001748 throw;
1749 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00001750#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiseliercd71f442017-01-07 03:01:24 +00001751 __deallocate_node(__cache);
Howard Hinnant3e519522010-05-11 19:42:16 +00001752 }
1753 for (; __first != __last; ++__first)
1754 __insert_unique(*__first);
1755}
1756
1757template <class _Tp, class _Hash, class _Equal, class _Alloc>
1758template <class _InputIterator>
1759void
1760__hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_multi(_InputIterator __first,
1761 _InputIterator __last)
1762{
Eric Fiselierfcd02212016-02-11 11:59:44 +00001763 typedef iterator_traits<_InputIterator> _ITraits;
1764 typedef typename _ITraits::value_type _ItValueType;
1765 static_assert((is_same<_ItValueType, __container_value_type>::value ||
1766 is_same<_ItValueType, __node_value_type>::value),
1767 "__assign_multi may only be called with the containers value type"
1768 " or the nodes value type");
Howard Hinnant3e519522010-05-11 19:42:16 +00001769 if (bucket_count() != 0)
1770 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001771 __next_pointer __cache = __detach();
Howard Hinnant3e519522010-05-11 19:42:16 +00001772#ifndef _LIBCPP_NO_EXCEPTIONS
1773 try
1774 {
Howard Hinnantb3371f62010-08-22 00:02:43 +00001775#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001776 for (; __cache != nullptr && __first != __last; ++__first)
1777 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001778 __cache->__upcast()->__value_ = *__first;
1779 __next_pointer __next = __cache->__next_;
1780 __node_insert_multi(__cache->__upcast());
Howard Hinnant3e519522010-05-11 19:42:16 +00001781 __cache = __next;
1782 }
1783#ifndef _LIBCPP_NO_EXCEPTIONS
1784 }
1785 catch (...)
1786 {
Eric Fiseliercd71f442017-01-07 03:01:24 +00001787 __deallocate_node(__cache);
Howard Hinnant3e519522010-05-11 19:42:16 +00001788 throw;
1789 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00001790#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiseliercd71f442017-01-07 03:01:24 +00001791 __deallocate_node(__cache);
Howard Hinnant3e519522010-05-11 19:42:16 +00001792 }
1793 for (; __first != __last; ++__first)
Eric Fiselierfcd02212016-02-11 11:59:44 +00001794 __insert_multi(_NodeTypes::__get_value(*__first));
Howard Hinnant3e519522010-05-11 19:42:16 +00001795}
1796
1797template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001798inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001799typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
Howard Hinnant37141072011-06-04 18:54:24 +00001800__hash_table<_Tp, _Hash, _Equal, _Alloc>::begin() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001801{
Howard Hinnantb24c8022013-07-23 22:01:58 +00001802#if _LIBCPP_DEBUG_LEVEL >= 2
1803 return iterator(__p1_.first().__next_, this);
1804#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001805 return iterator(__p1_.first().__next_);
Howard Hinnantb24c8022013-07-23 22:01:58 +00001806#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001807}
1808
1809template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001810inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001811typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
Howard Hinnant37141072011-06-04 18:54:24 +00001812__hash_table<_Tp, _Hash, _Equal, _Alloc>::end() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001813{
Howard Hinnantb24c8022013-07-23 22:01:58 +00001814#if _LIBCPP_DEBUG_LEVEL >= 2
1815 return iterator(nullptr, this);
1816#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001817 return iterator(nullptr);
Howard Hinnantb24c8022013-07-23 22:01:58 +00001818#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001819}
1820
1821template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001822inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001823typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
Howard Hinnant37141072011-06-04 18:54:24 +00001824__hash_table<_Tp, _Hash, _Equal, _Alloc>::begin() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001825{
Howard Hinnantb24c8022013-07-23 22:01:58 +00001826#if _LIBCPP_DEBUG_LEVEL >= 2
1827 return const_iterator(__p1_.first().__next_, this);
1828#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001829 return const_iterator(__p1_.first().__next_);
Howard Hinnantb24c8022013-07-23 22:01:58 +00001830#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001831}
1832
1833template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001834inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001835typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
Howard Hinnant37141072011-06-04 18:54:24 +00001836__hash_table<_Tp, _Hash, _Equal, _Alloc>::end() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001837{
Howard Hinnantb24c8022013-07-23 22:01:58 +00001838#if _LIBCPP_DEBUG_LEVEL >= 2
1839 return const_iterator(nullptr, this);
1840#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001841 return const_iterator(nullptr);
Howard Hinnantb24c8022013-07-23 22:01:58 +00001842#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001843}
1844
1845template <class _Tp, class _Hash, class _Equal, class _Alloc>
1846void
Howard Hinnant37141072011-06-04 18:54:24 +00001847__hash_table<_Tp, _Hash, _Equal, _Alloc>::clear() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001848{
1849 if (size() > 0)
1850 {
Eric Fiseliercd71f442017-01-07 03:01:24 +00001851 __deallocate_node(__p1_.first().__next_);
Howard Hinnant3e519522010-05-11 19:42:16 +00001852 __p1_.first().__next_ = nullptr;
1853 size_type __bc = bucket_count();
Howard Hinnant1f8da842011-07-05 14:14:17 +00001854 for (size_type __i = 0; __i < __bc; ++__i)
Howard Hinnant3e519522010-05-11 19:42:16 +00001855 __bucket_list_[__i] = nullptr;
1856 size() = 0;
1857 }
1858}
1859
Erik Pilkington5c4e07a2018-10-31 17:31:35 +00001860
1861// Prepare the container for an insertion of the value __value with the hash
1862// __hash. This does a lookup into the container to see if __value is already
1863// present, and performs a rehash if necessary. Returns a pointer to the
1864// existing element if it exists, otherwise nullptr.
1865//
1866// Note that this function does forward exceptions if key_eq() throws, and never
1867// mutates __value or actually inserts into the map.
Howard Hinnant3e519522010-05-11 19:42:16 +00001868template <class _Tp, class _Hash, class _Equal, class _Alloc>
Erik Pilkington5c4e07a2018-10-31 17:31:35 +00001869_LIBCPP_INLINE_VISIBILITY
1870typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__next_pointer
1871__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_unique_prepare(
1872 size_t __hash, value_type& __value)
Howard Hinnant3e519522010-05-11 19:42:16 +00001873{
Howard Hinnant3e519522010-05-11 19:42:16 +00001874 size_type __bc = bucket_count();
Erik Pilkington5c4e07a2018-10-31 17:31:35 +00001875
Howard Hinnant3e519522010-05-11 19:42:16 +00001876 if (__bc != 0)
1877 {
Erik Pilkington5c4e07a2018-10-31 17:31:35 +00001878 size_t __chash = __constrain_hash(__hash, __bc);
1879 __next_pointer __ndptr = __bucket_list_[__chash];
Howard Hinnant3e519522010-05-11 19:42:16 +00001880 if (__ndptr != nullptr)
1881 {
1882 for (__ndptr = __ndptr->__next_; __ndptr != nullptr &&
Eric Fiselier40492ba2016-07-23 20:36:55 +00001883 __constrain_hash(__ndptr->__hash(), __bc) == __chash;
Howard Hinnant3e519522010-05-11 19:42:16 +00001884 __ndptr = __ndptr->__next_)
1885 {
Erik Pilkington5c4e07a2018-10-31 17:31:35 +00001886 if (key_eq()(__ndptr->__upcast()->__value_, __value))
1887 return __ndptr;
Howard Hinnant3e519522010-05-11 19:42:16 +00001888 }
1889 }
1890 }
Erik Pilkington5c4e07a2018-10-31 17:31:35 +00001891 if (size()+1 > __bc * max_load_factor() || __bc == 0)
Howard Hinnant3e519522010-05-11 19:42:16 +00001892 {
Erik Pilkington5c4e07a2018-10-31 17:31:35 +00001893 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
1894 size_type(ceil(float(size() + 1) / max_load_factor()))));
Howard Hinnant3e519522010-05-11 19:42:16 +00001895 }
Erik Pilkington5c4e07a2018-10-31 17:31:35 +00001896 return nullptr;
1897}
1898
1899// Insert the node __nd into the container by pushing it into the right bucket,
1900// and updating size(). Assumes that __nd->__hash is up-to-date, and that
1901// rehashing has already occurred and that no element with the same key exists
1902// in the map.
1903template <class _Tp, class _Hash, class _Equal, class _Alloc>
1904_LIBCPP_INLINE_VISIBILITY
1905void
1906__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_unique_perform(
1907 __node_pointer __nd) _NOEXCEPT
1908{
1909 size_type __bc = bucket_count();
1910 size_t __chash = __constrain_hash(__nd->__hash(), __bc);
1911 // insert_after __bucket_list_[__chash], or __first_node if bucket is null
1912 __next_pointer __pn = __bucket_list_[__chash];
1913 if (__pn == nullptr)
1914 {
1915 __pn =__p1_.first().__ptr();
1916 __nd->__next_ = __pn->__next_;
1917 __pn->__next_ = __nd->__ptr();
1918 // fix up __bucket_list_
1919 __bucket_list_[__chash] = __pn;
1920 if (__nd->__next_ != nullptr)
1921 __bucket_list_[__constrain_hash(__nd->__next_->__hash(), __bc)] = __nd->__ptr();
1922 }
1923 else
1924 {
1925 __nd->__next_ = __pn->__next_;
1926 __pn->__next_ = __nd->__ptr();
1927 }
1928 ++size();
Howard Hinnant3e519522010-05-11 19:42:16 +00001929}
1930
1931template <class _Tp, class _Hash, class _Equal, class _Alloc>
Erik Pilkington5c4e07a2018-10-31 17:31:35 +00001932pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
1933__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_unique(__node_pointer __nd)
Howard Hinnant3e519522010-05-11 19:42:16 +00001934{
Erik Pilkington5c4e07a2018-10-31 17:31:35 +00001935 __nd->__hash_ = hash_function()(__nd->__value_);
1936 __next_pointer __existing_node =
1937 __node_insert_unique_prepare(__nd->__hash(), __nd->__value_);
1938
1939 // Insert the node, unless it already exists in the container.
1940 bool __inserted = false;
1941 if (__existing_node == nullptr)
1942 {
1943 __node_insert_unique_perform(__nd);
1944 __existing_node = __nd->__ptr();
1945 __inserted = true;
1946 }
1947#if _LIBCPP_DEBUG_LEVEL >= 2
1948 return pair<iterator, bool>(iterator(__existing_node, this), __inserted);
1949#else
1950 return pair<iterator, bool>(iterator(__existing_node), __inserted);
1951#endif
1952}
1953
1954// Prepare the container for an insertion of the value __cp_val with the hash
1955// __cp_hash. This does a lookup into the container to see if __cp_value is
1956// already present, and performs a rehash if necessary. Returns a pointer to the
1957// last occurance of __cp_val in the map.
1958//
1959// Note that this function does forward exceptions if key_eq() throws, and never
1960// mutates __value or actually inserts into the map.
1961template <class _Tp, class _Hash, class _Equal, class _Alloc>
1962typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__next_pointer
1963__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi_prepare(
1964 size_t __cp_hash, value_type& __cp_val)
1965{
Howard Hinnant3e519522010-05-11 19:42:16 +00001966 size_type __bc = bucket_count();
1967 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1968 {
Eric Fiselier9ba5c112015-02-02 21:31:48 +00001969 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnant3e519522010-05-11 19:42:16 +00001970 size_type(ceil(float(size() + 1) / max_load_factor()))));
1971 __bc = bucket_count();
1972 }
Erik Pilkington5c4e07a2018-10-31 17:31:35 +00001973 size_t __chash = __constrain_hash(__cp_hash, __bc);
Eric Fiselier40492ba2016-07-23 20:36:55 +00001974 __next_pointer __pn = __bucket_list_[__chash];
Erik Pilkington5c4e07a2018-10-31 17:31:35 +00001975 if (__pn != nullptr)
1976 {
1977 for (bool __found = false; __pn->__next_ != nullptr &&
1978 __constrain_hash(__pn->__next_->__hash(), __bc) == __chash;
1979 __pn = __pn->__next_)
1980 {
1981 // __found key_eq() action
1982 // false false loop
1983 // true true loop
1984 // false true set __found to true
1985 // true false break
1986 if (__found != (__pn->__next_->__hash() == __cp_hash &&
1987 key_eq()(__pn->__next_->__upcast()->__value_, __cp_val)))
1988 {
1989 if (!__found)
1990 __found = true;
1991 else
1992 break;
1993 }
1994 }
1995 }
1996 return __pn;
1997}
1998
1999// Insert the node __cp into the container after __pn (which is the last node in
2000// the bucket that compares equal to __cp). Rehashing, and checking for
2001// uniqueness has already been performed (in __node_insert_multi_prepare), so
2002// all we need to do is update the bucket and size(). Assumes that __cp->__hash
2003// is up-to-date.
2004template <class _Tp, class _Hash, class _Equal, class _Alloc>
2005void
2006__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi_perform(
2007 __node_pointer __cp, __next_pointer __pn) _NOEXCEPT
2008{
2009 size_type __bc = bucket_count();
2010 size_t __chash = __constrain_hash(__cp->__hash_, __bc);
Howard Hinnant3e519522010-05-11 19:42:16 +00002011 if (__pn == nullptr)
2012 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00002013 __pn =__p1_.first().__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00002014 __cp->__next_ = __pn->__next_;
Eric Fiselier40492ba2016-07-23 20:36:55 +00002015 __pn->__next_ = __cp->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00002016 // fix up __bucket_list_
2017 __bucket_list_[__chash] = __pn;
2018 if (__cp->__next_ != nullptr)
Eric Fiselier40492ba2016-07-23 20:36:55 +00002019 __bucket_list_[__constrain_hash(__cp->__next_->__hash(), __bc)]
2020 = __cp->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00002021 }
2022 else
2023 {
Howard Hinnant3e519522010-05-11 19:42:16 +00002024 __cp->__next_ = __pn->__next_;
Eric Fiselier40492ba2016-07-23 20:36:55 +00002025 __pn->__next_ = __cp->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00002026 if (__cp->__next_ != nullptr)
2027 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00002028 size_t __nhash = __constrain_hash(__cp->__next_->__hash(), __bc);
Howard Hinnant3e519522010-05-11 19:42:16 +00002029 if (__nhash != __chash)
Eric Fiselier40492ba2016-07-23 20:36:55 +00002030 __bucket_list_[__nhash] = __cp->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00002031 }
2032 }
2033 ++size();
Erik Pilkington5c4e07a2018-10-31 17:31:35 +00002034}
2035
2036
2037template <class _Tp, class _Hash, class _Equal, class _Alloc>
2038typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2039__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi(__node_pointer __cp)
2040{
2041 __cp->__hash_ = hash_function()(__cp->__value_);
2042 __next_pointer __pn = __node_insert_multi_prepare(__cp->__hash(), __cp->__value_);
2043 __node_insert_multi_perform(__cp, __pn);
2044
Howard Hinnantb24c8022013-07-23 22:01:58 +00002045#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselier40492ba2016-07-23 20:36:55 +00002046 return iterator(__cp->__ptr(), this);
Howard Hinnantb24c8022013-07-23 22:01:58 +00002047#else
Eric Fiselier40492ba2016-07-23 20:36:55 +00002048 return iterator(__cp->__ptr());
Howard Hinnantb24c8022013-07-23 22:01:58 +00002049#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002050}
2051
2052template <class _Tp, class _Hash, class _Equal, class _Alloc>
2053typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2054__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi(
2055 const_iterator __p, __node_pointer __cp)
2056{
Howard Hinnant2f51de52013-08-02 17:50:49 +00002057#if _LIBCPP_DEBUG_LEVEL >= 2
2058 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2059 "unordered container::emplace_hint(const_iterator, args...) called with an iterator not"
2060 " referring to this unordered container");
2061#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002062 if (__p != end() && key_eq()(*__p, __cp->__value_))
2063 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00002064 __next_pointer __np = __p.__node_;
2065 __cp->__hash_ = __np->__hash();
Howard Hinnant3e519522010-05-11 19:42:16 +00002066 size_type __bc = bucket_count();
2067 if (size()+1 > __bc * max_load_factor() || __bc == 0)
2068 {
Eric Fiselier9ba5c112015-02-02 21:31:48 +00002069 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnant3e519522010-05-11 19:42:16 +00002070 size_type(ceil(float(size() + 1) / max_load_factor()))));
2071 __bc = bucket_count();
2072 }
Howard Hinnant4cb38a82012-07-06 17:31:14 +00002073 size_t __chash = __constrain_hash(__cp->__hash_, __bc);
Eric Fiselier40492ba2016-07-23 20:36:55 +00002074 __next_pointer __pp = __bucket_list_[__chash];
Howard Hinnant3e519522010-05-11 19:42:16 +00002075 while (__pp->__next_ != __np)
2076 __pp = __pp->__next_;
2077 __cp->__next_ = __np;
Eric Fiselier40492ba2016-07-23 20:36:55 +00002078 __pp->__next_ = static_cast<__next_pointer>(__cp);
Howard Hinnant3e519522010-05-11 19:42:16 +00002079 ++size();
Howard Hinnantb24c8022013-07-23 22:01:58 +00002080#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselier40492ba2016-07-23 20:36:55 +00002081 return iterator(static_cast<__next_pointer>(__cp), this);
Howard Hinnantb24c8022013-07-23 22:01:58 +00002082#else
Eric Fiselier40492ba2016-07-23 20:36:55 +00002083 return iterator(static_cast<__next_pointer>(__cp));
Howard Hinnantb24c8022013-07-23 22:01:58 +00002084#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002085 }
2086 return __node_insert_multi(__cp);
2087}
2088
Eric Fiselierde3f2b32015-06-13 07:18:32 +00002089
2090
Eric Fiselierfcd02212016-02-11 11:59:44 +00002091#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierde3f2b32015-06-13 07:18:32 +00002092template <class _Tp, class _Hash, class _Equal, class _Alloc>
Eric Fiselierfcd02212016-02-11 11:59:44 +00002093template <class _Key, class ..._Args>
Eric Fiselierde3f2b32015-06-13 07:18:32 +00002094pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
Eric Fiselierfcd02212016-02-11 11:59:44 +00002095__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_key_args(_Key const& __k, _Args&&... __args)
Eric Fiselierde3f2b32015-06-13 07:18:32 +00002096#else
2097template <class _Tp, class _Hash, class _Equal, class _Alloc>
Eric Fiselierfcd02212016-02-11 11:59:44 +00002098template <class _Key, class _Args>
Eric Fiselierde3f2b32015-06-13 07:18:32 +00002099pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
Eric Fiselierfcd02212016-02-11 11:59:44 +00002100__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_key_args(_Key const& __k, _Args& __args)
Eric Fiselierde3f2b32015-06-13 07:18:32 +00002101#endif
2102{
Eric Fiselierfcd02212016-02-11 11:59:44 +00002103
2104 size_t __hash = hash_function()(__k);
Howard Hinnant3e519522010-05-11 19:42:16 +00002105 size_type __bc = bucket_count();
2106 bool __inserted = false;
Eric Fiselier40492ba2016-07-23 20:36:55 +00002107 __next_pointer __nd;
Howard Hinnant3e519522010-05-11 19:42:16 +00002108 size_t __chash;
2109 if (__bc != 0)
2110 {
Howard Hinnant4cb38a82012-07-06 17:31:14 +00002111 __chash = __constrain_hash(__hash, __bc);
Howard Hinnant3e519522010-05-11 19:42:16 +00002112 __nd = __bucket_list_[__chash];
2113 if (__nd != nullptr)
2114 {
2115 for (__nd = __nd->__next_; __nd != nullptr &&
Eric Fiselier1a06fe52016-07-24 06:22:25 +00002116 (__nd->__hash() == __hash || __constrain_hash(__nd->__hash(), __bc) == __chash);
Howard Hinnant3e519522010-05-11 19:42:16 +00002117 __nd = __nd->__next_)
2118 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00002119 if (key_eq()(__nd->__upcast()->__value_, __k))
Howard Hinnant3e519522010-05-11 19:42:16 +00002120 goto __done;
2121 }
2122 }
2123 }
2124 {
Eric Fiselierfcd02212016-02-11 11:59:44 +00002125#ifndef _LIBCPP_CXX03_LANG
2126 __node_holder __h = __construct_node_hash(__hash, _VSTD::forward<_Args>(__args)...);
2127#else
2128 __node_holder __h = __construct_node_hash(__hash, __args);
2129#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002130 if (size()+1 > __bc * max_load_factor() || __bc == 0)
2131 {
Eric Fiselier9ba5c112015-02-02 21:31:48 +00002132 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnant3e519522010-05-11 19:42:16 +00002133 size_type(ceil(float(size() + 1) / max_load_factor()))));
2134 __bc = bucket_count();
Howard Hinnant4cb38a82012-07-06 17:31:14 +00002135 __chash = __constrain_hash(__hash, __bc);
Howard Hinnant3e519522010-05-11 19:42:16 +00002136 }
2137 // insert_after __bucket_list_[__chash], or __first_node if bucket is null
Eric Fiselier40492ba2016-07-23 20:36:55 +00002138 __next_pointer __pn = __bucket_list_[__chash];
Howard Hinnant3e519522010-05-11 19:42:16 +00002139 if (__pn == nullptr)
2140 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00002141 __pn = __p1_.first().__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00002142 __h->__next_ = __pn->__next_;
Eric Fiselier40492ba2016-07-23 20:36:55 +00002143 __pn->__next_ = __h.get()->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00002144 // fix up __bucket_list_
2145 __bucket_list_[__chash] = __pn;
2146 if (__h->__next_ != nullptr)
Eric Fiselier40492ba2016-07-23 20:36:55 +00002147 __bucket_list_[__constrain_hash(__h->__next_->__hash(), __bc)]
2148 = __h.get()->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00002149 }
2150 else
2151 {
2152 __h->__next_ = __pn->__next_;
Eric Fiselier40492ba2016-07-23 20:36:55 +00002153 __pn->__next_ = static_cast<__next_pointer>(__h.get());
Howard Hinnant3e519522010-05-11 19:42:16 +00002154 }
Eric Fiselier40492ba2016-07-23 20:36:55 +00002155 __nd = static_cast<__next_pointer>(__h.release());
Howard Hinnant3e519522010-05-11 19:42:16 +00002156 // increment size
2157 ++size();
2158 __inserted = true;
2159 }
2160__done:
Howard Hinnantb24c8022013-07-23 22:01:58 +00002161#if _LIBCPP_DEBUG_LEVEL >= 2
2162 return pair<iterator, bool>(iterator(__nd, this), __inserted);
2163#else
Howard Hinnant3e519522010-05-11 19:42:16 +00002164 return pair<iterator, bool>(iterator(__nd), __inserted);
Howard Hinnantb24c8022013-07-23 22:01:58 +00002165#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002166}
2167
Eric Fiselierfcd02212016-02-11 11:59:44 +00002168#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00002169
2170template <class _Tp, class _Hash, class _Equal, class _Alloc>
2171template <class... _Args>
2172pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
Duncan P. N. Exon Smithfde79b42016-03-17 20:45:20 +00002173__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_impl(_Args&&... __args)
Howard Hinnant3e519522010-05-11 19:42:16 +00002174{
Howard Hinnantce48a112011-06-30 21:18:19 +00002175 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnant3e519522010-05-11 19:42:16 +00002176 pair<iterator, bool> __r = __node_insert_unique(__h.get());
2177 if (__r.second)
2178 __h.release();
2179 return __r;
2180}
2181
2182template <class _Tp, class _Hash, class _Equal, class _Alloc>
2183template <class... _Args>
2184typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2185__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_multi(_Args&&... __args)
2186{
Howard Hinnantce48a112011-06-30 21:18:19 +00002187 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnant3e519522010-05-11 19:42:16 +00002188 iterator __r = __node_insert_multi(__h.get());
2189 __h.release();
2190 return __r;
2191}
2192
2193template <class _Tp, class _Hash, class _Equal, class _Alloc>
2194template <class... _Args>
2195typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2196__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_hint_multi(
2197 const_iterator __p, _Args&&... __args)
2198{
Howard Hinnante5c13de2013-07-29 19:05:47 +00002199#if _LIBCPP_DEBUG_LEVEL >= 2
2200 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2201 "unordered container::emplace_hint(const_iterator, args...) called with an iterator not"
2202 " referring to this unordered container");
2203#endif
Howard Hinnantce48a112011-06-30 21:18:19 +00002204 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnant3e519522010-05-11 19:42:16 +00002205 iterator __r = __node_insert_multi(__p, __h.get());
2206 __h.release();
2207 return __r;
2208}
2209
Eric Fiselierfcd02212016-02-11 11:59:44 +00002210#else // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00002211
2212template <class _Tp, class _Hash, class _Equal, class _Alloc>
2213typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
Eric Fiselierfcd02212016-02-11 11:59:44 +00002214__hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const __container_value_type& __x)
Howard Hinnant3e519522010-05-11 19:42:16 +00002215{
2216 __node_holder __h = __construct_node(__x);
2217 iterator __r = __node_insert_multi(__h.get());
2218 __h.release();
2219 return __r;
2220}
2221
2222template <class _Tp, class _Hash, class _Equal, class _Alloc>
2223typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2224__hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const_iterator __p,
Eric Fiselierfcd02212016-02-11 11:59:44 +00002225 const __container_value_type& __x)
Howard Hinnant3e519522010-05-11 19:42:16 +00002226{
Howard Hinnante5c13de2013-07-29 19:05:47 +00002227#if _LIBCPP_DEBUG_LEVEL >= 2
2228 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2229 "unordered container::insert(const_iterator, lvalue) called with an iterator not"
2230 " referring to this unordered container");
2231#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002232 __node_holder __h = __construct_node(__x);
2233 iterator __r = __node_insert_multi(__p, __h.get());
2234 __h.release();
2235 return __r;
2236}
2237
Eric Fiselierfcd02212016-02-11 11:59:44 +00002238#endif // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00002239
Erik Pilkingtonb0386a52018-08-01 01:33:38 +00002240#if _LIBCPP_STD_VER > 14
2241template <class _Tp, class _Hash, class _Equal, class _Alloc>
2242template <class _NodeHandle, class _InsertReturnType>
2243_LIBCPP_INLINE_VISIBILITY
2244_InsertReturnType
2245__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_insert_unique(
2246 _NodeHandle&& __nh)
2247{
2248 if (__nh.empty())
2249 return _InsertReturnType{end(), false, _NodeHandle()};
2250 pair<iterator, bool> __result = __node_insert_unique(__nh.__ptr_);
2251 if (__result.second)
2252 __nh.__release();
2253 return _InsertReturnType{__result.first, __result.second, _VSTD::move(__nh)};
2254}
2255
2256template <class _Tp, class _Hash, class _Equal, class _Alloc>
2257template <class _NodeHandle>
2258_LIBCPP_INLINE_VISIBILITY
2259typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2260__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_insert_unique(
2261 const_iterator, _NodeHandle&& __nh)
2262{
2263 if (__nh.empty())
2264 return end();
2265 pair<iterator, bool> __result = __node_insert_unique(__nh.__ptr_);
2266 if (__result.second)
2267 __nh.__release();
2268 return __result.first;
2269}
2270
2271template <class _Tp, class _Hash, class _Equal, class _Alloc>
2272template <class _NodeHandle>
2273_LIBCPP_INLINE_VISIBILITY
2274_NodeHandle
2275__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_extract(
2276 key_type const& __key)
2277{
2278 iterator __i = find(__key);
2279 if (__i == end())
2280 return _NodeHandle();
2281 return __node_handle_extract<_NodeHandle>(__i);
2282}
2283
2284template <class _Tp, class _Hash, class _Equal, class _Alloc>
2285template <class _NodeHandle>
2286_LIBCPP_INLINE_VISIBILITY
2287_NodeHandle
2288__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_extract(
2289 const_iterator __p)
2290{
2291 allocator_type __alloc(__node_alloc());
2292 return _NodeHandle(remove(__p).release(), __alloc);
2293}
2294
2295template <class _Tp, class _Hash, class _Equal, class _Alloc>
Erik Pilkington5c4e07a2018-10-31 17:31:35 +00002296template <class _Table>
2297_LIBCPP_INLINE_VISIBILITY
2298void
2299__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_merge_unique(
2300 _Table& __source)
2301{
2302 static_assert(is_same<__node, typename _Table::__node>::value, "");
2303
2304 for (typename _Table::iterator __it = __source.begin();
2305 __it != __source.end();)
2306 {
2307 __node_pointer __src_ptr = __it.__node_->__upcast();
2308 size_t __hash = hash_function()(__src_ptr->__value_);
2309 __next_pointer __existing_node =
2310 __node_insert_unique_prepare(__hash, __src_ptr->__value_);
2311 auto __prev_iter = __it++;
2312 if (__existing_node == nullptr)
2313 {
2314 (void)__source.remove(__prev_iter).release();
2315 __src_ptr->__hash_ = __hash;
2316 __node_insert_unique_perform(__src_ptr);
2317 }
2318 }
2319}
2320
2321template <class _Tp, class _Hash, class _Equal, class _Alloc>
Erik Pilkingtonb0386a52018-08-01 01:33:38 +00002322template <class _NodeHandle>
2323_LIBCPP_INLINE_VISIBILITY
2324typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2325__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_insert_multi(
2326 _NodeHandle&& __nh)
2327{
2328 if (__nh.empty())
2329 return end();
2330 iterator __result = __node_insert_multi(__nh.__ptr_);
2331 __nh.__release();
2332 return __result;
2333}
2334
2335template <class _Tp, class _Hash, class _Equal, class _Alloc>
2336template <class _NodeHandle>
2337_LIBCPP_INLINE_VISIBILITY
2338typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2339__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_insert_multi(
2340 const_iterator __hint, _NodeHandle&& __nh)
2341{
2342 if (__nh.empty())
2343 return end();
2344 iterator __result = __node_insert_multi(__hint, __nh.__ptr_);
2345 __nh.__release();
2346 return __result;
2347}
2348
Erik Pilkington5c4e07a2018-10-31 17:31:35 +00002349template <class _Tp, class _Hash, class _Equal, class _Alloc>
2350template <class _Table>
2351_LIBCPP_INLINE_VISIBILITY
2352void
2353__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_merge_multi(
2354 _Table& __source)
2355{
2356 static_assert(is_same<typename _Table::__node, __node>::value, "");
2357
2358 for (typename _Table::iterator __it = __source.begin();
2359 __it != __source.end();)
2360 {
2361 __node_pointer __src_ptr = __it.__node_->__upcast();
2362 size_t __src_hash = hash_function()(__src_ptr->__value_);
2363 __next_pointer __pn =
2364 __node_insert_multi_prepare(__src_hash, __src_ptr->__value_);
2365 (void)__source.remove(__it++).release();
2366 __src_ptr->__hash_ = __src_hash;
2367 __node_insert_multi_perform(__src_ptr, __pn);
2368 }
2369}
Erik Pilkingtonb0386a52018-08-01 01:33:38 +00002370#endif // _LIBCPP_STD_VER > 14
2371
Howard Hinnant3e519522010-05-11 19:42:16 +00002372template <class _Tp, class _Hash, class _Equal, class _Alloc>
2373void
2374__hash_table<_Tp, _Hash, _Equal, _Alloc>::rehash(size_type __n)
2375{
Dan Albert553b09b2018-01-08 22:57:12 +00002376 if (__n == 1)
Howard Hinnant4cb38a82012-07-06 17:31:14 +00002377 __n = 2;
2378 else if (__n & (__n - 1))
2379 __n = __next_prime(__n);
Howard Hinnant3e519522010-05-11 19:42:16 +00002380 size_type __bc = bucket_count();
2381 if (__n > __bc)
2382 __rehash(__n);
Howard Hinnant4cb38a82012-07-06 17:31:14 +00002383 else if (__n < __bc)
Howard Hinnant3e519522010-05-11 19:42:16 +00002384 {
Howard Hinnantce48a112011-06-30 21:18:19 +00002385 __n = _VSTD::max<size_type>
Howard Hinnant3e519522010-05-11 19:42:16 +00002386 (
2387 __n,
Eric Fiselier9ba5c112015-02-02 21:31:48 +00002388 __is_hash_power2(__bc) ? __next_hash_pow2(size_t(ceil(float(size()) / max_load_factor()))) :
2389 __next_prime(size_t(ceil(float(size()) / max_load_factor())))
Howard Hinnant3e519522010-05-11 19:42:16 +00002390 );
2391 if (__n < __bc)
2392 __rehash(__n);
2393 }
2394}
2395
2396template <class _Tp, class _Hash, class _Equal, class _Alloc>
2397void
2398__hash_table<_Tp, _Hash, _Equal, _Alloc>::__rehash(size_type __nbc)
2399{
Howard Hinnantb24c8022013-07-23 22:01:58 +00002400#if _LIBCPP_DEBUG_LEVEL >= 2
2401 __get_db()->__invalidate_all(this);
2402#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant3e519522010-05-11 19:42:16 +00002403 __pointer_allocator& __npa = __bucket_list_.get_deleter().__alloc();
2404 __bucket_list_.reset(__nbc > 0 ?
2405 __pointer_alloc_traits::allocate(__npa, __nbc) : nullptr);
2406 __bucket_list_.get_deleter().size() = __nbc;
2407 if (__nbc > 0)
2408 {
2409 for (size_type __i = 0; __i < __nbc; ++__i)
2410 __bucket_list_[__i] = nullptr;
Eric Fiselier40492ba2016-07-23 20:36:55 +00002411 __next_pointer __pp = __p1_.first().__ptr();
2412 __next_pointer __cp = __pp->__next_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002413 if (__cp != nullptr)
2414 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00002415 size_type __chash = __constrain_hash(__cp->__hash(), __nbc);
Howard Hinnant3e519522010-05-11 19:42:16 +00002416 __bucket_list_[__chash] = __pp;
2417 size_type __phash = __chash;
2418 for (__pp = __cp, __cp = __cp->__next_; __cp != nullptr;
2419 __cp = __pp->__next_)
2420 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00002421 __chash = __constrain_hash(__cp->__hash(), __nbc);
Howard Hinnant3e519522010-05-11 19:42:16 +00002422 if (__chash == __phash)
2423 __pp = __cp;
2424 else
2425 {
2426 if (__bucket_list_[__chash] == nullptr)
2427 {
2428 __bucket_list_[__chash] = __pp;
2429 __pp = __cp;
2430 __phash = __chash;
2431 }
2432 else
2433 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00002434 __next_pointer __np = __cp;
Howard Hinnant3e519522010-05-11 19:42:16 +00002435 for (; __np->__next_ != nullptr &&
Eric Fiselier40492ba2016-07-23 20:36:55 +00002436 key_eq()(__cp->__upcast()->__value_,
2437 __np->__next_->__upcast()->__value_);
Howard Hinnant3e519522010-05-11 19:42:16 +00002438 __np = __np->__next_)
2439 ;
2440 __pp->__next_ = __np->__next_;
2441 __np->__next_ = __bucket_list_[__chash]->__next_;
2442 __bucket_list_[__chash]->__next_ = __cp;
Howard Hinnantb3371f62010-08-22 00:02:43 +00002443
Howard Hinnant3e519522010-05-11 19:42:16 +00002444 }
2445 }
2446 }
2447 }
2448 }
2449}
2450
2451template <class _Tp, class _Hash, class _Equal, class _Alloc>
2452template <class _Key>
2453typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2454__hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k)
2455{
2456 size_t __hash = hash_function()(__k);
2457 size_type __bc = bucket_count();
2458 if (__bc != 0)
2459 {
Howard Hinnant4cb38a82012-07-06 17:31:14 +00002460 size_t __chash = __constrain_hash(__hash, __bc);
Eric Fiselier40492ba2016-07-23 20:36:55 +00002461 __next_pointer __nd = __bucket_list_[__chash];
Howard Hinnant3e519522010-05-11 19:42:16 +00002462 if (__nd != nullptr)
2463 {
2464 for (__nd = __nd->__next_; __nd != nullptr &&
Eric Fiselier40492ba2016-07-23 20:36:55 +00002465 (__nd->__hash() == __hash
2466 || __constrain_hash(__nd->__hash(), __bc) == __chash);
Howard Hinnant3e519522010-05-11 19:42:16 +00002467 __nd = __nd->__next_)
2468 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00002469 if ((__nd->__hash() == __hash)
2470 && key_eq()(__nd->__upcast()->__value_, __k))
Howard Hinnantb24c8022013-07-23 22:01:58 +00002471#if _LIBCPP_DEBUG_LEVEL >= 2
2472 return iterator(__nd, this);
2473#else
Howard Hinnant3e519522010-05-11 19:42:16 +00002474 return iterator(__nd);
Howard Hinnantb24c8022013-07-23 22:01:58 +00002475#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002476 }
2477 }
2478 }
2479 return end();
2480}
2481
2482template <class _Tp, class _Hash, class _Equal, class _Alloc>
2483template <class _Key>
2484typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
2485__hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k) const
2486{
2487 size_t __hash = hash_function()(__k);
2488 size_type __bc = bucket_count();
2489 if (__bc != 0)
2490 {
Howard Hinnant4cb38a82012-07-06 17:31:14 +00002491 size_t __chash = __constrain_hash(__hash, __bc);
Eric Fiselier40492ba2016-07-23 20:36:55 +00002492 __next_pointer __nd = __bucket_list_[__chash];
Howard Hinnant3e519522010-05-11 19:42:16 +00002493 if (__nd != nullptr)
2494 {
2495 for (__nd = __nd->__next_; __nd != nullptr &&
Eric Fiselier40492ba2016-07-23 20:36:55 +00002496 (__hash == __nd->__hash()
2497 || __constrain_hash(__nd->__hash(), __bc) == __chash);
Howard Hinnant3e519522010-05-11 19:42:16 +00002498 __nd = __nd->__next_)
2499 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00002500 if ((__nd->__hash() == __hash)
2501 && key_eq()(__nd->__upcast()->__value_, __k))
Howard Hinnantb24c8022013-07-23 22:01:58 +00002502#if _LIBCPP_DEBUG_LEVEL >= 2
2503 return const_iterator(__nd, this);
2504#else
Howard Hinnant3e519522010-05-11 19:42:16 +00002505 return const_iterator(__nd);
Howard Hinnantb24c8022013-07-23 22:01:58 +00002506#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002507 }
2508 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00002509
Howard Hinnant3e519522010-05-11 19:42:16 +00002510 }
2511 return end();
2512}
2513
Eric Fiselierfcd02212016-02-11 11:59:44 +00002514#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00002515
2516template <class _Tp, class _Hash, class _Equal, class _Alloc>
2517template <class ..._Args>
2518typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
2519__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(_Args&& ...__args)
2520{
Eric Fiselierfcd02212016-02-11 11:59:44 +00002521 static_assert(!__is_hash_value_type<_Args...>::value,
2522 "Construct cannot be called with a hash value type");
Howard Hinnant3e519522010-05-11 19:42:16 +00002523 __node_allocator& __na = __node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00002524 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselierfcd02212016-02-11 11:59:44 +00002525 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), _VSTD::forward<_Args>(__args)...);
Howard Hinnant3e519522010-05-11 19:42:16 +00002526 __h.get_deleter().__value_constructed = true;
2527 __h->__hash_ = hash_function()(__h->__value_);
2528 __h->__next_ = nullptr;
2529 return __h;
2530}
2531
2532template <class _Tp, class _Hash, class _Equal, class _Alloc>
Eric Fiselierfcd02212016-02-11 11:59:44 +00002533template <class _First, class ..._Rest>
Howard Hinnant3e519522010-05-11 19:42:16 +00002534typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Eric Fiselierfcd02212016-02-11 11:59:44 +00002535__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node_hash(
2536 size_t __hash, _First&& __f, _Rest&& ...__rest)
Howard Hinnant3e519522010-05-11 19:42:16 +00002537{
Eric Fiselierfcd02212016-02-11 11:59:44 +00002538 static_assert(!__is_hash_value_type<_First, _Rest...>::value,
2539 "Construct cannot be called with a hash value type");
Howard Hinnant3e519522010-05-11 19:42:16 +00002540 __node_allocator& __na = __node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00002541 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselierfcd02212016-02-11 11:59:44 +00002542 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_),
2543 _VSTD::forward<_First>(__f),
2544 _VSTD::forward<_Rest>(__rest)...);
Howard Hinnant3e519522010-05-11 19:42:16 +00002545 __h.get_deleter().__value_constructed = true;
2546 __h->__hash_ = __hash;
2547 __h->__next_ = nullptr;
Howard Hinnant179b1f82013-08-22 18:29:50 +00002548 return __h;
Howard Hinnant3e519522010-05-11 19:42:16 +00002549}
2550
Eric Fiselierfcd02212016-02-11 11:59:44 +00002551#else // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00002552
2553template <class _Tp, class _Hash, class _Equal, class _Alloc>
2554typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Eric Fiselierfcd02212016-02-11 11:59:44 +00002555__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(const __container_value_type& __v)
Howard Hinnant3e519522010-05-11 19:42:16 +00002556{
2557 __node_allocator& __na = __node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00002558 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselierfcd02212016-02-11 11:59:44 +00002559 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), __v);
Howard Hinnant3e519522010-05-11 19:42:16 +00002560 __h.get_deleter().__value_constructed = true;
2561 __h->__hash_ = hash_function()(__h->__value_);
2562 __h->__next_ = nullptr;
Dimitry Andric251c6292015-08-19 06:43:33 +00002563 return _LIBCPP_EXPLICIT_MOVE(__h); // explicitly moved for C++03
Howard Hinnant3e519522010-05-11 19:42:16 +00002564}
2565
Howard Hinnant3e519522010-05-11 19:42:16 +00002566template <class _Tp, class _Hash, class _Equal, class _Alloc>
2567typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Eric Fiselierfcd02212016-02-11 11:59:44 +00002568__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node_hash(size_t __hash,
2569 const __container_value_type& __v)
Howard Hinnant3e519522010-05-11 19:42:16 +00002570{
2571 __node_allocator& __na = __node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00002572 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselierfcd02212016-02-11 11:59:44 +00002573 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), __v);
Howard Hinnant3e519522010-05-11 19:42:16 +00002574 __h.get_deleter().__value_constructed = true;
2575 __h->__hash_ = __hash;
2576 __h->__next_ = nullptr;
Dimitry Andric251c6292015-08-19 06:43:33 +00002577 return _LIBCPP_EXPLICIT_MOVE(__h); // explicitly moved for C++03
Howard Hinnant3e519522010-05-11 19:42:16 +00002578}
2579
Eric Fiselierfcd02212016-02-11 11:59:44 +00002580#endif // _LIBCPP_CXX03_LANG
2581
Howard Hinnant3e519522010-05-11 19:42:16 +00002582template <class _Tp, class _Hash, class _Equal, class _Alloc>
2583typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2584__hash_table<_Tp, _Hash, _Equal, _Alloc>::erase(const_iterator __p)
2585{
Eric Fiselier40492ba2016-07-23 20:36:55 +00002586 __next_pointer __np = __p.__node_;
Howard Hinnantb24c8022013-07-23 22:01:58 +00002587#if _LIBCPP_DEBUG_LEVEL >= 2
2588 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2589 "unordered container erase(iterator) called with an iterator not"
2590 " referring to this container");
2591 _LIBCPP_ASSERT(__p != end(),
2592 "unordered container erase(iterator) called with a non-dereferenceable iterator");
2593 iterator __r(__np, this);
2594#else
Howard Hinnant3e519522010-05-11 19:42:16 +00002595 iterator __r(__np);
Howard Hinnantb24c8022013-07-23 22:01:58 +00002596#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002597 ++__r;
2598 remove(__p);
2599 return __r;
2600}
2601
2602template <class _Tp, class _Hash, class _Equal, class _Alloc>
2603typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2604__hash_table<_Tp, _Hash, _Equal, _Alloc>::erase(const_iterator __first,
2605 const_iterator __last)
2606{
Howard Hinnantb24c8022013-07-23 22:01:58 +00002607#if _LIBCPP_DEBUG_LEVEL >= 2
2608 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
2609 "unodered container::erase(iterator, iterator) called with an iterator not"
2610 " referring to this unodered container");
2611 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__last) == this,
2612 "unodered container::erase(iterator, iterator) called with an iterator not"
2613 " referring to this unodered container");
2614#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002615 for (const_iterator __p = __first; __first != __last; __p = __first)
2616 {
2617 ++__first;
2618 erase(__p);
2619 }
Eric Fiselier40492ba2016-07-23 20:36:55 +00002620 __next_pointer __np = __last.__node_;
Howard Hinnantb24c8022013-07-23 22:01:58 +00002621#if _LIBCPP_DEBUG_LEVEL >= 2
2622 return iterator (__np, this);
2623#else
Howard Hinnant3e519522010-05-11 19:42:16 +00002624 return iterator (__np);
Howard Hinnantb24c8022013-07-23 22:01:58 +00002625#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002626}
2627
2628template <class _Tp, class _Hash, class _Equal, class _Alloc>
2629template <class _Key>
2630typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2631__hash_table<_Tp, _Hash, _Equal, _Alloc>::__erase_unique(const _Key& __k)
2632{
2633 iterator __i = find(__k);
2634 if (__i == end())
2635 return 0;
2636 erase(__i);
2637 return 1;
2638}
2639
2640template <class _Tp, class _Hash, class _Equal, class _Alloc>
2641template <class _Key>
2642typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2643__hash_table<_Tp, _Hash, _Equal, _Alloc>::__erase_multi(const _Key& __k)
2644{
2645 size_type __r = 0;
2646 iterator __i = find(__k);
2647 if (__i != end())
2648 {
2649 iterator __e = end();
2650 do
2651 {
2652 erase(__i++);
2653 ++__r;
2654 } while (__i != __e && key_eq()(*__i, __k));
2655 }
2656 return __r;
2657}
2658
2659template <class _Tp, class _Hash, class _Equal, class _Alloc>
2660typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Howard Hinnant37141072011-06-04 18:54:24 +00002661__hash_table<_Tp, _Hash, _Equal, _Alloc>::remove(const_iterator __p) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00002662{
2663 // current node
Eric Fiselier40492ba2016-07-23 20:36:55 +00002664 __next_pointer __cn = __p.__node_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002665 size_type __bc = bucket_count();
Eric Fiselier40492ba2016-07-23 20:36:55 +00002666 size_t __chash = __constrain_hash(__cn->__hash(), __bc);
Howard Hinnant3e519522010-05-11 19:42:16 +00002667 // find previous node
Eric Fiselier40492ba2016-07-23 20:36:55 +00002668 __next_pointer __pn = __bucket_list_[__chash];
Howard Hinnant3e519522010-05-11 19:42:16 +00002669 for (; __pn->__next_ != __cn; __pn = __pn->__next_)
2670 ;
2671 // Fix up __bucket_list_
2672 // if __pn is not in same bucket (before begin is not in same bucket) &&
2673 // if __cn->__next_ is not in same bucket (nullptr is not in same bucket)
Eric Fiselier40492ba2016-07-23 20:36:55 +00002674 if (__pn == __p1_.first().__ptr()
2675 || __constrain_hash(__pn->__hash(), __bc) != __chash)
Howard Hinnant3e519522010-05-11 19:42:16 +00002676 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00002677 if (__cn->__next_ == nullptr
2678 || __constrain_hash(__cn->__next_->__hash(), __bc) != __chash)
Howard Hinnant3e519522010-05-11 19:42:16 +00002679 __bucket_list_[__chash] = nullptr;
2680 }
2681 // if __cn->__next_ is not in same bucket (nullptr is in same bucket)
2682 if (__cn->__next_ != nullptr)
2683 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00002684 size_t __nhash = __constrain_hash(__cn->__next_->__hash(), __bc);
Howard Hinnant3e519522010-05-11 19:42:16 +00002685 if (__nhash != __chash)
2686 __bucket_list_[__nhash] = __pn;
2687 }
2688 // remove __cn
2689 __pn->__next_ = __cn->__next_;
2690 __cn->__next_ = nullptr;
2691 --size();
Howard Hinnantb24c8022013-07-23 22:01:58 +00002692#if _LIBCPP_DEBUG_LEVEL >= 2
2693 __c_node* __c = __get_db()->__find_c_and_lock(this);
Eric Fiselier780b51d2016-12-28 05:53:01 +00002694 for (__i_node** __dp = __c->end_; __dp != __c->beg_; )
Howard Hinnantb24c8022013-07-23 22:01:58 +00002695 {
Eric Fiselier780b51d2016-12-28 05:53:01 +00002696 --__dp;
2697 iterator* __i = static_cast<iterator*>((*__dp)->__i_);
Howard Hinnantb24c8022013-07-23 22:01:58 +00002698 if (__i->__node_ == __cn)
2699 {
Eric Fiselier780b51d2016-12-28 05:53:01 +00002700 (*__dp)->__c_ = nullptr;
2701 if (--__c->end_ != __dp)
2702 memmove(__dp, __dp+1, (__c->end_ - __dp)*sizeof(__i_node*));
Howard Hinnantb24c8022013-07-23 22:01:58 +00002703 }
2704 }
2705 __get_db()->unlock();
2706#endif
Eric Fiselier40492ba2016-07-23 20:36:55 +00002707 return __node_holder(__cn->__upcast(), _Dp(__node_alloc(), true));
Howard Hinnant3e519522010-05-11 19:42:16 +00002708}
2709
2710template <class _Tp, class _Hash, class _Equal, class _Alloc>
2711template <class _Key>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00002712inline
Howard Hinnant3e519522010-05-11 19:42:16 +00002713typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2714__hash_table<_Tp, _Hash, _Equal, _Alloc>::__count_unique(const _Key& __k) const
2715{
2716 return static_cast<size_type>(find(__k) != end());
2717}
2718
2719template <class _Tp, class _Hash, class _Equal, class _Alloc>
2720template <class _Key>
2721typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2722__hash_table<_Tp, _Hash, _Equal, _Alloc>::__count_multi(const _Key& __k) const
2723{
2724 size_type __r = 0;
2725 const_iterator __i = find(__k);
2726 if (__i != end())
2727 {
2728 const_iterator __e = end();
2729 do
2730 {
2731 ++__i;
2732 ++__r;
2733 } while (__i != __e && key_eq()(*__i, __k));
2734 }
2735 return __r;
2736}
2737
2738template <class _Tp, class _Hash, class _Equal, class _Alloc>
2739template <class _Key>
2740pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator,
2741 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator>
2742__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_unique(
2743 const _Key& __k)
2744{
2745 iterator __i = find(__k);
2746 iterator __j = __i;
2747 if (__i != end())
2748 ++__j;
2749 return pair<iterator, iterator>(__i, __j);
2750}
2751
2752template <class _Tp, class _Hash, class _Equal, class _Alloc>
2753template <class _Key>
2754pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator,
2755 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator>
2756__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_unique(
2757 const _Key& __k) const
2758{
2759 const_iterator __i = find(__k);
2760 const_iterator __j = __i;
2761 if (__i != end())
2762 ++__j;
2763 return pair<const_iterator, const_iterator>(__i, __j);
2764}
2765
2766template <class _Tp, class _Hash, class _Equal, class _Alloc>
2767template <class _Key>
2768pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator,
2769 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator>
2770__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_multi(
2771 const _Key& __k)
2772{
2773 iterator __i = find(__k);
2774 iterator __j = __i;
2775 if (__i != end())
2776 {
2777 iterator __e = end();
2778 do
2779 {
2780 ++__j;
2781 } while (__j != __e && key_eq()(*__j, __k));
2782 }
2783 return pair<iterator, iterator>(__i, __j);
2784}
2785
2786template <class _Tp, class _Hash, class _Equal, class _Alloc>
2787template <class _Key>
2788pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator,
2789 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator>
2790__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_multi(
2791 const _Key& __k) const
2792{
2793 const_iterator __i = find(__k);
2794 const_iterator __j = __i;
2795 if (__i != end())
2796 {
2797 const_iterator __e = end();
2798 do
2799 {
2800 ++__j;
2801 } while (__j != __e && key_eq()(*__j, __k));
2802 }
2803 return pair<const_iterator, const_iterator>(__i, __j);
2804}
2805
2806template <class _Tp, class _Hash, class _Equal, class _Alloc>
2807void
2808__hash_table<_Tp, _Hash, _Equal, _Alloc>::swap(__hash_table& __u)
Eric Fiselier87a82492015-07-18 20:40:46 +00002809#if _LIBCPP_STD_VER <= 11
Eric Fiselier780b51d2016-12-28 05:53:01 +00002810 _NOEXCEPT_DEBUG_(
Marshall Clowe3fbe142015-07-13 20:04:56 +00002811 __is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value
Marshall Clowe3fbe142015-07-13 20:04:56 +00002812 && (!allocator_traits<__pointer_allocator>::propagate_on_container_swap::value
2813 || __is_nothrow_swappable<__pointer_allocator>::value)
2814 && (!__node_traits::propagate_on_container_swap::value
2815 || __is_nothrow_swappable<__node_allocator>::value)
Marshall Clowe3fbe142015-07-13 20:04:56 +00002816 )
Eric Fiselier87a82492015-07-18 20:40:46 +00002817#else
Eric Fiselier780b51d2016-12-28 05:53:01 +00002818 _NOEXCEPT_DEBUG_(__is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value)
Eric Fiselier87a82492015-07-18 20:40:46 +00002819#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002820{
Eric Fiselier780b51d2016-12-28 05:53:01 +00002821 _LIBCPP_ASSERT(__node_traits::propagate_on_container_swap::value ||
2822 this->__node_alloc() == __u.__node_alloc(),
2823 "list::swap: Either propagate_on_container_swap must be true"
2824 " or the allocators must compare equal");
Howard Hinnant3e519522010-05-11 19:42:16 +00002825 {
2826 __node_pointer_pointer __npp = __bucket_list_.release();
2827 __bucket_list_.reset(__u.__bucket_list_.release());
2828 __u.__bucket_list_.reset(__npp);
2829 }
Howard Hinnantce48a112011-06-30 21:18:19 +00002830 _VSTD::swap(__bucket_list_.get_deleter().size(), __u.__bucket_list_.get_deleter().size());
Marshall Clowe3fbe142015-07-13 20:04:56 +00002831 __swap_allocator(__bucket_list_.get_deleter().__alloc(),
Howard Hinnant3e519522010-05-11 19:42:16 +00002832 __u.__bucket_list_.get_deleter().__alloc());
Marshall Clowe3fbe142015-07-13 20:04:56 +00002833 __swap_allocator(__node_alloc(), __u.__node_alloc());
Howard Hinnantce48a112011-06-30 21:18:19 +00002834 _VSTD::swap(__p1_.first().__next_, __u.__p1_.first().__next_);
Howard Hinnant3e519522010-05-11 19:42:16 +00002835 __p2_.swap(__u.__p2_);
2836 __p3_.swap(__u.__p3_);
2837 if (size() > 0)
Eric Fiselier40492ba2016-07-23 20:36:55 +00002838 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash(), bucket_count())] =
2839 __p1_.first().__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00002840 if (__u.size() > 0)
Eric Fiselier40492ba2016-07-23 20:36:55 +00002841 __u.__bucket_list_[__constrain_hash(__u.__p1_.first().__next_->__hash(), __u.bucket_count())] =
2842 __u.__p1_.first().__ptr();
Howard Hinnantb24c8022013-07-23 22:01:58 +00002843#if _LIBCPP_DEBUG_LEVEL >= 2
2844 __get_db()->swap(this, &__u);
2845#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002846}
2847
2848template <class _Tp, class _Hash, class _Equal, class _Alloc>
2849typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2850__hash_table<_Tp, _Hash, _Equal, _Alloc>::bucket_size(size_type __n) const
2851{
Howard Hinnante5c13de2013-07-29 19:05:47 +00002852 _LIBCPP_ASSERT(__n < bucket_count(),
2853 "unordered container::bucket_size(n) called with n >= bucket_count()");
Eric Fiselier40492ba2016-07-23 20:36:55 +00002854 __next_pointer __np = __bucket_list_[__n];
Howard Hinnant3e519522010-05-11 19:42:16 +00002855 size_type __bc = bucket_count();
2856 size_type __r = 0;
2857 if (__np != nullptr)
2858 {
2859 for (__np = __np->__next_; __np != nullptr &&
Eric Fiselier40492ba2016-07-23 20:36:55 +00002860 __constrain_hash(__np->__hash(), __bc) == __n;
Howard Hinnant3e519522010-05-11 19:42:16 +00002861 __np = __np->__next_, ++__r)
2862 ;
2863 }
2864 return __r;
2865}
2866
Howard Hinnant37141072011-06-04 18:54:24 +00002867template <class _Tp, class _Hash, class _Equal, class _Alloc>
2868inline _LIBCPP_INLINE_VISIBILITY
2869void
2870swap(__hash_table<_Tp, _Hash, _Equal, _Alloc>& __x,
2871 __hash_table<_Tp, _Hash, _Equal, _Alloc>& __y)
2872 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
2873{
2874 __x.swap(__y);
2875}
2876
Howard Hinnantb24c8022013-07-23 22:01:58 +00002877#if _LIBCPP_DEBUG_LEVEL >= 2
2878
2879template <class _Tp, class _Hash, class _Equal, class _Alloc>
2880bool
2881__hash_table<_Tp, _Hash, _Equal, _Alloc>::__dereferenceable(const const_iterator* __i) const
2882{
2883 return __i->__node_ != nullptr;
2884}
2885
2886template <class _Tp, class _Hash, class _Equal, class _Alloc>
2887bool
2888__hash_table<_Tp, _Hash, _Equal, _Alloc>::__decrementable(const const_iterator*) const
2889{
2890 return false;
2891}
2892
2893template <class _Tp, class _Hash, class _Equal, class _Alloc>
2894bool
2895__hash_table<_Tp, _Hash, _Equal, _Alloc>::__addable(const const_iterator*, ptrdiff_t) const
2896{
2897 return false;
2898}
2899
2900template <class _Tp, class _Hash, class _Equal, class _Alloc>
2901bool
2902__hash_table<_Tp, _Hash, _Equal, _Alloc>::__subscriptable(const const_iterator*, ptrdiff_t) const
2903{
2904 return false;
2905}
2906
2907#endif // _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiseliera016efb2017-05-31 22:07:49 +00002908
Howard Hinnant3e519522010-05-11 19:42:16 +00002909_LIBCPP_END_NAMESPACE_STD
2910
Eric Fiseliera016efb2017-05-31 22:07:49 +00002911_LIBCPP_POP_MACROS
2912
Howard Hinnant3e519522010-05-11 19:42:16 +00002913#endif // _LIBCPP__HASH_TABLE