blob: f6b789dc75a2f8a5fea2711343ce5f65839a7256 [file] [log] [blame]
Howard Hinnant3e519522010-05-11 19:42:16 +00001// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
Chandler Carruth57b08b02019-01-19 10:56:40 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnant3e519522010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP__HASH_TABLE
11#define _LIBCPP__HASH_TABLE
12
13#include <__config>
14#include <initializer_list>
15#include <memory>
16#include <iterator>
17#include <algorithm>
18#include <cmath>
Eric Fiselier75d0dcf2016-02-10 20:46:23 +000019#include <utility>
Eric Fiselier04333f92017-01-13 22:42:53 +000020#include <type_traits>
Howard Hinnant3e519522010-05-11 19:42:16 +000021
Eric Fiselierc1bd9192014-08-10 23:53:08 +000022#include <__debug>
Howard Hinnant42a30462013-08-02 00:26:35 +000023
Howard Hinnant073458b2011-10-17 20:05:10 +000024#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnant3e519522010-05-11 19:42:16 +000025#pragma GCC system_header
Howard Hinnant073458b2011-10-17 20:05:10 +000026#endif
Howard Hinnant3e519522010-05-11 19:42:16 +000027
Eric Fiseliera016efb2017-05-31 22:07:49 +000028_LIBCPP_PUSH_MACROS
29#include <__undef_macros>
Howard Hinnant3e519522010-05-11 19:42:16 +000030
Eric Fiselierfcd02212016-02-11 11:59:44 +000031
Eric Fiseliera016efb2017-05-31 22:07:49 +000032_LIBCPP_BEGIN_NAMESPACE_STD
33
Eric Fiselierfcd02212016-02-11 11:59:44 +000034template <class _Key, class _Tp>
35struct __hash_value_type;
Eric Fiselierfcd02212016-02-11 11:59:44 +000036
37#ifndef _LIBCPP_CXX03_LANG
38template <class _Tp>
39struct __is_hash_value_type_imp : false_type {};
40
41template <class _Key, class _Value>
42struct __is_hash_value_type_imp<__hash_value_type<_Key, _Value>> : true_type {};
43
44template <class ..._Args>
45struct __is_hash_value_type : false_type {};
46
47template <class _One>
48struct __is_hash_value_type<_One> : __is_hash_value_type_imp<typename __uncvref<_One>::type> {};
49#endif
50
Howard Hinnant6e412562013-03-06 23:30:19 +000051_LIBCPP_FUNC_VIS
Howard Hinnantce534202011-06-14 19:58:17 +000052size_t __next_prime(size_t __n);
Howard Hinnant3e519522010-05-11 19:42:16 +000053
54template <class _NodePtr>
55struct __hash_node_base
56{
Eric Fiselier40492ba2016-07-23 20:36:55 +000057 typedef typename pointer_traits<_NodePtr>::element_type __node_type;
Howard Hinnant3e519522010-05-11 19:42:16 +000058 typedef __hash_node_base __first_node;
Eric Fiselier40492ba2016-07-23 20:36:55 +000059 typedef typename __rebind_pointer<_NodePtr, __first_node>::type __node_base_pointer;
60 typedef _NodePtr __node_pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +000061
Eric Fiselier40492ba2016-07-23 20:36:55 +000062#if defined(_LIBCPP_ABI_FIX_UNORDERED_NODE_POINTER_UB)
63 typedef __node_base_pointer __next_pointer;
64#else
65 typedef typename conditional<
66 is_pointer<__node_pointer>::value,
67 __node_base_pointer,
68 __node_pointer>::type __next_pointer;
69#endif
70
71 __next_pointer __next_;
72
73 _LIBCPP_INLINE_VISIBILITY
74 __next_pointer __ptr() _NOEXCEPT {
75 return static_cast<__next_pointer>(
76 pointer_traits<__node_base_pointer>::pointer_to(*this));
77 }
78
79 _LIBCPP_INLINE_VISIBILITY
80 __node_pointer __upcast() _NOEXCEPT {
81 return static_cast<__node_pointer>(
82 pointer_traits<__node_base_pointer>::pointer_to(*this));
83 }
84
85 _LIBCPP_INLINE_VISIBILITY
86 size_t __hash() const _NOEXCEPT {
87 return static_cast<__node_type const&>(*this).__hash_;
88 }
Howard Hinnant3e519522010-05-11 19:42:16 +000089
Howard Hinnant37141072011-06-04 18:54:24 +000090 _LIBCPP_INLINE_VISIBILITY __hash_node_base() _NOEXCEPT : __next_(nullptr) {}
Howard Hinnant3e519522010-05-11 19:42:16 +000091};
92
93template <class _Tp, class _VoidPtr>
94struct __hash_node
95 : public __hash_node_base
96 <
Eric Fiselier934b0922015-12-30 21:52:00 +000097 typename __rebind_pointer<_VoidPtr, __hash_node<_Tp, _VoidPtr> >::type
Howard Hinnant3e519522010-05-11 19:42:16 +000098 >
99{
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000100 typedef _Tp __node_value_type;
Howard Hinnant3e519522010-05-11 19:42:16 +0000101
Eric Fiselier40492ba2016-07-23 20:36:55 +0000102 size_t __hash_;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000103 __node_value_type __value_;
Howard Hinnant3e519522010-05-11 19:42:16 +0000104};
105
Howard Hinnant4cb38a82012-07-06 17:31:14 +0000106inline _LIBCPP_INLINE_VISIBILITY
107bool
Eric Fiselier9ba5c112015-02-02 21:31:48 +0000108__is_hash_power2(size_t __bc)
Howard Hinnant4cb38a82012-07-06 17:31:14 +0000109{
110 return __bc > 2 && !(__bc & (__bc - 1));
111}
112
113inline _LIBCPP_INLINE_VISIBILITY
114size_t
115__constrain_hash(size_t __h, size_t __bc)
116{
Eric Fiselier118cb412016-07-11 22:02:02 +0000117 return !(__bc & (__bc - 1)) ? __h & (__bc - 1) :
118 (__h < __bc ? __h : __h % __bc);
Howard Hinnant4cb38a82012-07-06 17:31:14 +0000119}
120
121inline _LIBCPP_INLINE_VISIBILITY
122size_t
Eric Fiselier9ba5c112015-02-02 21:31:48 +0000123__next_hash_pow2(size_t __n)
Howard Hinnant4cb38a82012-07-06 17:31:14 +0000124{
Marshall Clow87af6462017-06-03 00:08:32 +0000125 return __n < 2 ? __n : (size_t(1) << (std::numeric_limits<size_t>::digits - __clz(__n-1)));
Howard Hinnant4cb38a82012-07-06 17:31:14 +0000126}
127
Duncan P. N. Exon Smithfde79b42016-03-17 20:45:20 +0000128
Howard Hinnantce534202011-06-14 19:58:17 +0000129template <class _Tp, class _Hash, class _Equal, class _Alloc> class __hash_table;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000130
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +0000131template <class _NodePtr> class _LIBCPP_TEMPLATE_VIS __hash_iterator;
132template <class _ConstNodePtr> class _LIBCPP_TEMPLATE_VIS __hash_const_iterator;
133template <class _NodePtr> class _LIBCPP_TEMPLATE_VIS __hash_local_iterator;
134template <class _ConstNodePtr> class _LIBCPP_TEMPLATE_VIS __hash_const_local_iterator;
135template <class _HashIterator> class _LIBCPP_TEMPLATE_VIS __hash_map_iterator;
136template <class _HashIterator> class _LIBCPP_TEMPLATE_VIS __hash_map_const_iterator;
Howard Hinnant3e519522010-05-11 19:42:16 +0000137
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000138template <class _Tp>
Eric Fiselier43b121d2016-02-20 07:59:16 +0000139struct __hash_key_value_types {
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000140 static_assert(!is_reference<_Tp>::value && !is_const<_Tp>::value, "");
141 typedef _Tp key_type;
142 typedef _Tp __node_value_type;
143 typedef _Tp __container_value_type;
144 static const bool __is_map = false;
Eric Fiselierfcd02212016-02-11 11:59:44 +0000145
146 _LIBCPP_INLINE_VISIBILITY
147 static key_type const& __get_key(_Tp const& __v) {
148 return __v;
149 }
150 _LIBCPP_INLINE_VISIBILITY
151 static __container_value_type const& __get_value(__node_value_type const& __v) {
152 return __v;
153 }
154 _LIBCPP_INLINE_VISIBILITY
155 static __container_value_type* __get_ptr(__node_value_type& __n) {
156 return _VSTD::addressof(__n);
157 }
158#ifndef _LIBCPP_CXX03_LANG
159 _LIBCPP_INLINE_VISIBILITY
Erik Pilkingtonf52318b2018-06-04 20:38:23 +0000160 static __container_value_type&& __move(__node_value_type& __v) {
Eric Fiselierfcd02212016-02-11 11:59:44 +0000161 return _VSTD::move(__v);
162 }
163#endif
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000164};
165
166template <class _Key, class _Tp>
Eric Fiselier43b121d2016-02-20 07:59:16 +0000167struct __hash_key_value_types<__hash_value_type<_Key, _Tp> > {
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000168 typedef _Key key_type;
169 typedef _Tp mapped_type;
170 typedef __hash_value_type<_Key, _Tp> __node_value_type;
171 typedef pair<const _Key, _Tp> __container_value_type;
172 typedef __container_value_type __map_value_type;
173 static const bool __is_map = true;
Eric Fiselierfcd02212016-02-11 11:59:44 +0000174
175 _LIBCPP_INLINE_VISIBILITY
176 static key_type const& __get_key(__container_value_type const& __v) {
177 return __v.first;
178 }
179
180 template <class _Up>
181 _LIBCPP_INLINE_VISIBILITY
182 static typename enable_if<__is_same_uncvref<_Up, __node_value_type>::value,
183 __container_value_type const&>::type
184 __get_value(_Up& __t) {
Erik Pilkingtonf52318b2018-06-04 20:38:23 +0000185 return __t.__get_value();
Eric Fiselierfcd02212016-02-11 11:59:44 +0000186 }
187
188 template <class _Up>
189 _LIBCPP_INLINE_VISIBILITY
190 static typename enable_if<__is_same_uncvref<_Up, __container_value_type>::value,
191 __container_value_type const&>::type
192 __get_value(_Up& __t) {
193 return __t;
194 }
195
196 _LIBCPP_INLINE_VISIBILITY
197 static __container_value_type* __get_ptr(__node_value_type& __n) {
Erik Pilkingtonf52318b2018-06-04 20:38:23 +0000198 return _VSTD::addressof(__n.__get_value());
Eric Fiselierfcd02212016-02-11 11:59:44 +0000199 }
200#ifndef _LIBCPP_CXX03_LANG
201 _LIBCPP_INLINE_VISIBILITY
Erik Pilkingtonf52318b2018-06-04 20:38:23 +0000202 static pair<key_type&&, mapped_type&&> __move(__node_value_type& __v) {
203 return __v.__move();
Eric Fiselierfcd02212016-02-11 11:59:44 +0000204 }
205#endif
206
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000207};
208
Eric Fiselier43b121d2016-02-20 07:59:16 +0000209template <class _Tp, class _AllocPtr, class _KVTypes = __hash_key_value_types<_Tp>,
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000210 bool = _KVTypes::__is_map>
Eric Fiselier43b121d2016-02-20 07:59:16 +0000211struct __hash_map_pointer_types {};
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000212
213template <class _Tp, class _AllocPtr, class _KVTypes>
Eric Fiselier43b121d2016-02-20 07:59:16 +0000214struct __hash_map_pointer_types<_Tp, _AllocPtr, _KVTypes, true> {
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000215 typedef typename _KVTypes::__map_value_type _Mv;
216 typedef typename __rebind_pointer<_AllocPtr, _Mv>::type
217 __map_value_type_pointer;
218 typedef typename __rebind_pointer<_AllocPtr, const _Mv>::type
219 __const_map_value_type_pointer;
220};
221
222template <class _NodePtr, class _NodeT = typename pointer_traits<_NodePtr>::element_type>
223struct __hash_node_types;
224
225template <class _NodePtr, class _Tp, class _VoidPtr>
226struct __hash_node_types<_NodePtr, __hash_node<_Tp, _VoidPtr> >
Eric Fiselier43b121d2016-02-20 07:59:16 +0000227 : public __hash_key_value_types<_Tp>, __hash_map_pointer_types<_Tp, _VoidPtr>
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000228
229{
Eric Fiselier43b121d2016-02-20 07:59:16 +0000230 typedef __hash_key_value_types<_Tp> __base;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000231
232public:
233 typedef ptrdiff_t difference_type;
234 typedef size_t size_type;
235
236 typedef typename __rebind_pointer<_NodePtr, void>::type __void_pointer;
237
238 typedef typename pointer_traits<_NodePtr>::element_type __node_type;
239 typedef _NodePtr __node_pointer;
240
241 typedef __hash_node_base<__node_pointer> __node_base_type;
242 typedef typename __rebind_pointer<_NodePtr, __node_base_type>::type
243 __node_base_pointer;
244
Eric Fiselier40492ba2016-07-23 20:36:55 +0000245 typedef typename __node_base_type::__next_pointer __next_pointer;
246
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000247 typedef _Tp __node_value_type;
248 typedef typename __rebind_pointer<_VoidPtr, __node_value_type>::type
249 __node_value_type_pointer;
250 typedef typename __rebind_pointer<_VoidPtr, const __node_value_type>::type
251 __const_node_value_type_pointer;
Eric Fiselier40492ba2016-07-23 20:36:55 +0000252
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000253private:
254 static_assert(!is_const<__node_type>::value,
255 "_NodePtr should never be a pointer to const");
256 static_assert((is_same<typename pointer_traits<_VoidPtr>::element_type, void>::value),
257 "_VoidPtr does not point to unqualified void type");
258 static_assert((is_same<typename __rebind_pointer<_VoidPtr, __node_type>::type,
259 _NodePtr>::value), "_VoidPtr does not rebind to _NodePtr.");
260};
261
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000262template <class _HashIterator>
263struct __hash_node_types_from_iterator;
264template <class _NodePtr>
265struct __hash_node_types_from_iterator<__hash_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
266template <class _NodePtr>
267struct __hash_node_types_from_iterator<__hash_const_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
268template <class _NodePtr>
269struct __hash_node_types_from_iterator<__hash_local_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
270template <class _NodePtr>
271struct __hash_node_types_from_iterator<__hash_const_local_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
272
273
274template <class _NodeValueTp, class _VoidPtr>
275struct __make_hash_node_types {
276 typedef __hash_node<_NodeValueTp, _VoidPtr> _NodeTp;
277 typedef typename __rebind_pointer<_VoidPtr, _NodeTp>::type _NodePtr;
278 typedef __hash_node_types<_NodePtr> type;
279};
280
Howard Hinnant3e519522010-05-11 19:42:16 +0000281template <class _NodePtr>
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +0000282class _LIBCPP_TEMPLATE_VIS __hash_iterator
Howard Hinnant3e519522010-05-11 19:42:16 +0000283{
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000284 typedef __hash_node_types<_NodePtr> _NodeTypes;
Eric Fiselier40492ba2016-07-23 20:36:55 +0000285 typedef _NodePtr __node_pointer;
286 typedef typename _NodeTypes::__next_pointer __next_pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000287
Eric Fiselier40492ba2016-07-23 20:36:55 +0000288 __next_pointer __node_;
Howard Hinnant3e519522010-05-11 19:42:16 +0000289
290public:
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000291 typedef forward_iterator_tag iterator_category;
292 typedef typename _NodeTypes::__node_value_type value_type;
293 typedef typename _NodeTypes::difference_type difference_type;
294 typedef value_type& reference;
295 typedef typename _NodeTypes::__node_value_type_pointer pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000296
Eric Fiselier40492ba2016-07-23 20:36:55 +0000297 _LIBCPP_INLINE_VISIBILITY __hash_iterator() _NOEXCEPT : __node_(nullptr) {
298 _LIBCPP_DEBUG_MODE(__get_db()->__insert_i(this));
Howard Hinnantb24c8022013-07-23 22:01:58 +0000299 }
300
301#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant43d99232010-09-21 17:32:39 +0000302 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb24c8022013-07-23 22:01:58 +0000303 __hash_iterator(const __hash_iterator& __i)
304 : __node_(__i.__node_)
305 {
306 __get_db()->__iterator_copy(this, &__i);
307 }
308
Howard Hinnant43d99232010-09-21 17:32:39 +0000309 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb24c8022013-07-23 22:01:58 +0000310 ~__hash_iterator()
311 {
312 __get_db()->__erase_i(this);
313 }
314
315 _LIBCPP_INLINE_VISIBILITY
316 __hash_iterator& operator=(const __hash_iterator& __i)
317 {
318 if (this != &__i)
319 {
320 __get_db()->__iterator_copy(this, &__i);
321 __node_ = __i.__node_;
322 }
323 return *this;
324 }
Howard Hinnantb24c8022013-07-23 22:01:58 +0000325#endif // _LIBCPP_DEBUG_LEVEL >= 2
326
327 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000328 reference operator*() const {
329 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
330 "Attempted to dereference a non-dereferenceable unordered container iterator");
331 return __node_->__upcast()->__value_;
332 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000333
Howard Hinnant43d99232010-09-21 17:32:39 +0000334 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000335 pointer operator->() const {
336 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
337 "Attempted to dereference a non-dereferenceable unordered container iterator");
338 return pointer_traits<pointer>::pointer_to(__node_->__upcast()->__value_);
339 }
340
341 _LIBCPP_INLINE_VISIBILITY
342 __hash_iterator& operator++() {
343 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
Howard Hinnantb24c8022013-07-23 22:01:58 +0000344 "Attempted to increment non-incrementable unordered container iterator");
Howard Hinnant3e519522010-05-11 19:42:16 +0000345 __node_ = __node_->__next_;
346 return *this;
347 }
348
Howard Hinnant43d99232010-09-21 17:32:39 +0000349 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000350 __hash_iterator operator++(int)
351 {
352 __hash_iterator __t(*this);
353 ++(*this);
354 return __t;
355 }
356
Howard Hinnant43d99232010-09-21 17:32:39 +0000357 friend _LIBCPP_INLINE_VISIBILITY
358 bool operator==(const __hash_iterator& __x, const __hash_iterator& __y)
Howard Hinnantb24c8022013-07-23 22:01:58 +0000359 {
Howard Hinnantb24c8022013-07-23 22:01:58 +0000360 return __x.__node_ == __y.__node_;
361 }
Howard Hinnant43d99232010-09-21 17:32:39 +0000362 friend _LIBCPP_INLINE_VISIBILITY
363 bool operator!=(const __hash_iterator& __x, const __hash_iterator& __y)
Howard Hinnantb24c8022013-07-23 22:01:58 +0000364 {return !(__x == __y);}
Howard Hinnant3e519522010-05-11 19:42:16 +0000365
366private:
Howard Hinnantb24c8022013-07-23 22:01:58 +0000367#if _LIBCPP_DEBUG_LEVEL >= 2
368 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000369 __hash_iterator(__next_pointer __node, const void* __c) _NOEXCEPT
Howard Hinnantb24c8022013-07-23 22:01:58 +0000370 : __node_(__node)
371 {
372 __get_db()->__insert_ic(this, __c);
373 }
374#else
Howard Hinnant43d99232010-09-21 17:32:39 +0000375 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000376 __hash_iterator(__next_pointer __node) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000377 : __node_(__node)
378 {}
Howard Hinnantb24c8022013-07-23 22:01:58 +0000379#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000380 template <class, class, class, class> friend class __hash_table;
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +0000381 template <class> friend class _LIBCPP_TEMPLATE_VIS __hash_const_iterator;
382 template <class> friend class _LIBCPP_TEMPLATE_VIS __hash_map_iterator;
383 template <class, class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS unordered_map;
384 template <class, class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS unordered_multimap;
Howard Hinnant3e519522010-05-11 19:42:16 +0000385};
386
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000387template <class _NodePtr>
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +0000388class _LIBCPP_TEMPLATE_VIS __hash_const_iterator
Howard Hinnant3e519522010-05-11 19:42:16 +0000389{
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000390 static_assert(!is_const<typename pointer_traits<_NodePtr>::element_type>::value, "");
391 typedef __hash_node_types<_NodePtr> _NodeTypes;
Eric Fiselier40492ba2016-07-23 20:36:55 +0000392 typedef _NodePtr __node_pointer;
393 typedef typename _NodeTypes::__next_pointer __next_pointer;
Eric Fiselier757373e2016-02-18 00:20:34 +0000394
Eric Fiselier40492ba2016-07-23 20:36:55 +0000395 __next_pointer __node_;
Howard Hinnant3e519522010-05-11 19:42:16 +0000396
397public:
Eric Fiselier757373e2016-02-18 00:20:34 +0000398 typedef __hash_iterator<_NodePtr> __non_const_iterator;
399
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000400 typedef forward_iterator_tag iterator_category;
401 typedef typename _NodeTypes::__node_value_type value_type;
402 typedef typename _NodeTypes::difference_type difference_type;
403 typedef const value_type& reference;
404 typedef typename _NodeTypes::__const_node_value_type_pointer pointer;
405
Howard Hinnant3e519522010-05-11 19:42:16 +0000406
Eric Fiselier40492ba2016-07-23 20:36:55 +0000407 _LIBCPP_INLINE_VISIBILITY __hash_const_iterator() _NOEXCEPT : __node_(nullptr) {
408 _LIBCPP_DEBUG_MODE(__get_db()->__insert_i(this));
Howard Hinnantb24c8022013-07-23 22:01:58 +0000409 }
Eric Fiselier40492ba2016-07-23 20:36:55 +0000410
Louis Dionne3560fbf32018-12-06 21:46:17 +0000411 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +0000412 __hash_const_iterator(const __non_const_iterator& __x) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000413 : __node_(__x.__node_)
Howard Hinnantb24c8022013-07-23 22:01:58 +0000414 {
Eric Fiselier40492ba2016-07-23 20:36:55 +0000415 _LIBCPP_DEBUG_MODE(__get_db()->__iterator_copy(this, &__x));
Howard Hinnantb24c8022013-07-23 22:01:58 +0000416 }
417
418#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant43d99232010-09-21 17:32:39 +0000419 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb24c8022013-07-23 22:01:58 +0000420 __hash_const_iterator(const __hash_const_iterator& __i)
421 : __node_(__i.__node_)
422 {
423 __get_db()->__iterator_copy(this, &__i);
424 }
425
Howard Hinnant43d99232010-09-21 17:32:39 +0000426 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb24c8022013-07-23 22:01:58 +0000427 ~__hash_const_iterator()
428 {
429 __get_db()->__erase_i(this);
430 }
431
432 _LIBCPP_INLINE_VISIBILITY
433 __hash_const_iterator& operator=(const __hash_const_iterator& __i)
434 {
435 if (this != &__i)
436 {
437 __get_db()->__iterator_copy(this, &__i);
438 __node_ = __i.__node_;
439 }
440 return *this;
441 }
Howard Hinnantb24c8022013-07-23 22:01:58 +0000442#endif // _LIBCPP_DEBUG_LEVEL >= 2
443
444 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000445 reference operator*() const {
446 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
Howard Hinnantb24c8022013-07-23 22:01:58 +0000447 "Attempted to dereference a non-dereferenceable unordered container const_iterator");
Eric Fiselier40492ba2016-07-23 20:36:55 +0000448 return __node_->__upcast()->__value_;
449 }
Howard Hinnantb24c8022013-07-23 22:01:58 +0000450 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000451 pointer operator->() const {
452 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
Howard Hinnantb24c8022013-07-23 22:01:58 +0000453 "Attempted to dereference a non-dereferenceable unordered container const_iterator");
Eric Fiselier40492ba2016-07-23 20:36:55 +0000454 return pointer_traits<pointer>::pointer_to(__node_->__upcast()->__value_);
455 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000456
Howard Hinnant43d99232010-09-21 17:32:39 +0000457 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000458 __hash_const_iterator& operator++() {
459 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
460 "Attempted to increment non-incrementable unordered container const_iterator");
Howard Hinnant3e519522010-05-11 19:42:16 +0000461 __node_ = __node_->__next_;
462 return *this;
463 }
464
Howard Hinnant43d99232010-09-21 17:32:39 +0000465 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000466 __hash_const_iterator operator++(int)
467 {
468 __hash_const_iterator __t(*this);
469 ++(*this);
470 return __t;
471 }
472
Howard Hinnant43d99232010-09-21 17:32:39 +0000473 friend _LIBCPP_INLINE_VISIBILITY
474 bool operator==(const __hash_const_iterator& __x, const __hash_const_iterator& __y)
Howard Hinnantb24c8022013-07-23 22:01:58 +0000475 {
Howard Hinnantb24c8022013-07-23 22:01:58 +0000476 return __x.__node_ == __y.__node_;
477 }
Howard Hinnant43d99232010-09-21 17:32:39 +0000478 friend _LIBCPP_INLINE_VISIBILITY
479 bool operator!=(const __hash_const_iterator& __x, const __hash_const_iterator& __y)
Howard Hinnantb24c8022013-07-23 22:01:58 +0000480 {return !(__x == __y);}
Howard Hinnant3e519522010-05-11 19:42:16 +0000481
482private:
Howard Hinnantb24c8022013-07-23 22:01:58 +0000483#if _LIBCPP_DEBUG_LEVEL >= 2
484 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000485 __hash_const_iterator(__next_pointer __node, const void* __c) _NOEXCEPT
Howard Hinnantb24c8022013-07-23 22:01:58 +0000486 : __node_(__node)
487 {
488 __get_db()->__insert_ic(this, __c);
489 }
490#else
Howard Hinnant43d99232010-09-21 17:32:39 +0000491 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000492 __hash_const_iterator(__next_pointer __node) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000493 : __node_(__node)
494 {}
Howard Hinnantb24c8022013-07-23 22:01:58 +0000495#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000496 template <class, class, class, class> friend class __hash_table;
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +0000497 template <class> friend class _LIBCPP_TEMPLATE_VIS __hash_map_const_iterator;
498 template <class, class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS unordered_map;
499 template <class, class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS unordered_multimap;
Howard Hinnant3e519522010-05-11 19:42:16 +0000500};
501
Howard Hinnant3e519522010-05-11 19:42:16 +0000502template <class _NodePtr>
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +0000503class _LIBCPP_TEMPLATE_VIS __hash_local_iterator
Howard Hinnant3e519522010-05-11 19:42:16 +0000504{
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000505 typedef __hash_node_types<_NodePtr> _NodeTypes;
Eric Fiselier40492ba2016-07-23 20:36:55 +0000506 typedef _NodePtr __node_pointer;
507 typedef typename _NodeTypes::__next_pointer __next_pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000508
Eric Fiselier40492ba2016-07-23 20:36:55 +0000509 __next_pointer __node_;
Howard Hinnant3e519522010-05-11 19:42:16 +0000510 size_t __bucket_;
511 size_t __bucket_count_;
512
Howard Hinnant3e519522010-05-11 19:42:16 +0000513public:
514 typedef forward_iterator_tag iterator_category;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000515 typedef typename _NodeTypes::__node_value_type value_type;
516 typedef typename _NodeTypes::difference_type difference_type;
Howard Hinnant3e519522010-05-11 19:42:16 +0000517 typedef value_type& reference;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000518 typedef typename _NodeTypes::__node_value_type_pointer pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000519
Eric Fiselier40492ba2016-07-23 20:36:55 +0000520 _LIBCPP_INLINE_VISIBILITY __hash_local_iterator() _NOEXCEPT : __node_(nullptr) {
521 _LIBCPP_DEBUG_MODE(__get_db()->__insert_i(this));
Howard Hinnantb24c8022013-07-23 22:01:58 +0000522 }
523
524#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant43d99232010-09-21 17:32:39 +0000525 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb24c8022013-07-23 22:01:58 +0000526 __hash_local_iterator(const __hash_local_iterator& __i)
527 : __node_(__i.__node_),
528 __bucket_(__i.__bucket_),
529 __bucket_count_(__i.__bucket_count_)
530 {
531 __get_db()->__iterator_copy(this, &__i);
532 }
533
Howard Hinnant43d99232010-09-21 17:32:39 +0000534 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb24c8022013-07-23 22:01:58 +0000535 ~__hash_local_iterator()
536 {
537 __get_db()->__erase_i(this);
538 }
539
540 _LIBCPP_INLINE_VISIBILITY
541 __hash_local_iterator& operator=(const __hash_local_iterator& __i)
542 {
543 if (this != &__i)
544 {
545 __get_db()->__iterator_copy(this, &__i);
546 __node_ = __i.__node_;
547 __bucket_ = __i.__bucket_;
548 __bucket_count_ = __i.__bucket_count_;
549 }
550 return *this;
551 }
Howard Hinnantb24c8022013-07-23 22:01:58 +0000552#endif // _LIBCPP_DEBUG_LEVEL >= 2
553
554 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000555 reference operator*() const {
556 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
Howard Hinnantb24c8022013-07-23 22:01:58 +0000557 "Attempted to dereference a non-dereferenceable unordered container local_iterator");
Eric Fiselier40492ba2016-07-23 20:36:55 +0000558 return __node_->__upcast()->__value_;
559 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000560
Howard Hinnant43d99232010-09-21 17:32:39 +0000561 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000562 pointer operator->() const {
563 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
564 "Attempted to dereference a non-dereferenceable unordered container local_iterator");
565 return pointer_traits<pointer>::pointer_to(__node_->__upcast()->__value_);
566 }
567
568 _LIBCPP_INLINE_VISIBILITY
569 __hash_local_iterator& operator++() {
570 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
Howard Hinnantb24c8022013-07-23 22:01:58 +0000571 "Attempted to increment non-incrementable unordered container local_iterator");
Howard Hinnant3e519522010-05-11 19:42:16 +0000572 __node_ = __node_->__next_;
Eric Fiselier40492ba2016-07-23 20:36:55 +0000573 if (__node_ != nullptr && __constrain_hash(__node_->__hash(), __bucket_count_) != __bucket_)
Howard Hinnant3e519522010-05-11 19:42:16 +0000574 __node_ = nullptr;
575 return *this;
576 }
577
Howard Hinnant43d99232010-09-21 17:32:39 +0000578 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000579 __hash_local_iterator operator++(int)
580 {
581 __hash_local_iterator __t(*this);
582 ++(*this);
583 return __t;
584 }
585
Howard Hinnant43d99232010-09-21 17:32:39 +0000586 friend _LIBCPP_INLINE_VISIBILITY
587 bool operator==(const __hash_local_iterator& __x, const __hash_local_iterator& __y)
Howard Hinnantb24c8022013-07-23 22:01:58 +0000588 {
Howard Hinnantb24c8022013-07-23 22:01:58 +0000589 return __x.__node_ == __y.__node_;
590 }
Howard Hinnant43d99232010-09-21 17:32:39 +0000591 friend _LIBCPP_INLINE_VISIBILITY
592 bool operator!=(const __hash_local_iterator& __x, const __hash_local_iterator& __y)
Howard Hinnantb24c8022013-07-23 22:01:58 +0000593 {return !(__x == __y);}
Howard Hinnant3e519522010-05-11 19:42:16 +0000594
595private:
Howard Hinnantb24c8022013-07-23 22:01:58 +0000596#if _LIBCPP_DEBUG_LEVEL >= 2
597 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000598 __hash_local_iterator(__next_pointer __node, size_t __bucket,
Howard Hinnantb24c8022013-07-23 22:01:58 +0000599 size_t __bucket_count, const void* __c) _NOEXCEPT
600 : __node_(__node),
601 __bucket_(__bucket),
602 __bucket_count_(__bucket_count)
603 {
604 __get_db()->__insert_ic(this, __c);
605 if (__node_ != nullptr)
606 __node_ = __node_->__next_;
607 }
608#else
Howard Hinnant43d99232010-09-21 17:32:39 +0000609 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000610 __hash_local_iterator(__next_pointer __node, size_t __bucket,
Howard Hinnant37141072011-06-04 18:54:24 +0000611 size_t __bucket_count) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000612 : __node_(__node),
613 __bucket_(__bucket),
614 __bucket_count_(__bucket_count)
615 {
616 if (__node_ != nullptr)
617 __node_ = __node_->__next_;
618 }
Howard Hinnantb24c8022013-07-23 22:01:58 +0000619#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000620 template <class, class, class, class> friend class __hash_table;
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +0000621 template <class> friend class _LIBCPP_TEMPLATE_VIS __hash_const_local_iterator;
622 template <class> friend class _LIBCPP_TEMPLATE_VIS __hash_map_iterator;
Howard Hinnant3e519522010-05-11 19:42:16 +0000623};
624
625template <class _ConstNodePtr>
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +0000626class _LIBCPP_TEMPLATE_VIS __hash_const_local_iterator
Howard Hinnant3e519522010-05-11 19:42:16 +0000627{
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000628 typedef __hash_node_types<_ConstNodePtr> _NodeTypes;
Eric Fiselier40492ba2016-07-23 20:36:55 +0000629 typedef _ConstNodePtr __node_pointer;
630 typedef typename _NodeTypes::__next_pointer __next_pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000631
Eric Fiselier40492ba2016-07-23 20:36:55 +0000632 __next_pointer __node_;
Howard Hinnant3e519522010-05-11 19:42:16 +0000633 size_t __bucket_;
634 size_t __bucket_count_;
635
636 typedef pointer_traits<__node_pointer> __pointer_traits;
637 typedef typename __pointer_traits::element_type __node;
638 typedef typename remove_const<__node>::type __non_const_node;
Eric Fiselier934b0922015-12-30 21:52:00 +0000639 typedef typename __rebind_pointer<__node_pointer, __non_const_node>::type
640 __non_const_node_pointer;
Eric Fiselier757373e2016-02-18 00:20:34 +0000641public:
Howard Hinnant3e519522010-05-11 19:42:16 +0000642 typedef __hash_local_iterator<__non_const_node_pointer>
643 __non_const_iterator;
Eric Fiselier757373e2016-02-18 00:20:34 +0000644
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000645 typedef forward_iterator_tag iterator_category;
646 typedef typename _NodeTypes::__node_value_type value_type;
647 typedef typename _NodeTypes::difference_type difference_type;
648 typedef const value_type& reference;
649 typedef typename _NodeTypes::__const_node_value_type_pointer pointer;
Eric Fiselier934b0922015-12-30 21:52:00 +0000650
Howard Hinnant3e519522010-05-11 19:42:16 +0000651
Eric Fiselier40492ba2016-07-23 20:36:55 +0000652 _LIBCPP_INLINE_VISIBILITY __hash_const_local_iterator() _NOEXCEPT : __node_(nullptr) {
653 _LIBCPP_DEBUG_MODE(__get_db()->__insert_i(this));
Howard Hinnantb24c8022013-07-23 22:01:58 +0000654 }
655
Howard Hinnant43d99232010-09-21 17:32:39 +0000656 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +0000657 __hash_const_local_iterator(const __non_const_iterator& __x) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000658 : __node_(__x.__node_),
659 __bucket_(__x.__bucket_),
660 __bucket_count_(__x.__bucket_count_)
Howard Hinnantb24c8022013-07-23 22:01:58 +0000661 {
Eric Fiselier40492ba2016-07-23 20:36:55 +0000662 _LIBCPP_DEBUG_MODE(__get_db()->__iterator_copy(this, &__x));
Howard Hinnantb24c8022013-07-23 22:01:58 +0000663 }
664
665#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant43d99232010-09-21 17:32:39 +0000666 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb24c8022013-07-23 22:01:58 +0000667 __hash_const_local_iterator(const __hash_const_local_iterator& __i)
668 : __node_(__i.__node_),
669 __bucket_(__i.__bucket_),
670 __bucket_count_(__i.__bucket_count_)
671 {
672 __get_db()->__iterator_copy(this, &__i);
673 }
674
Howard Hinnant43d99232010-09-21 17:32:39 +0000675 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb24c8022013-07-23 22:01:58 +0000676 ~__hash_const_local_iterator()
677 {
678 __get_db()->__erase_i(this);
679 }
680
681 _LIBCPP_INLINE_VISIBILITY
682 __hash_const_local_iterator& operator=(const __hash_const_local_iterator& __i)
683 {
684 if (this != &__i)
685 {
686 __get_db()->__iterator_copy(this, &__i);
687 __node_ = __i.__node_;
688 __bucket_ = __i.__bucket_;
689 __bucket_count_ = __i.__bucket_count_;
690 }
691 return *this;
692 }
Howard Hinnantb24c8022013-07-23 22:01:58 +0000693#endif // _LIBCPP_DEBUG_LEVEL >= 2
694
695 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000696 reference operator*() const {
697 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
Howard Hinnantb24c8022013-07-23 22:01:58 +0000698 "Attempted to dereference a non-dereferenceable unordered container const_local_iterator");
Eric Fiselier40492ba2016-07-23 20:36:55 +0000699 return __node_->__upcast()->__value_;
700 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000701
Howard Hinnant43d99232010-09-21 17:32:39 +0000702 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000703 pointer operator->() const {
704 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
705 "Attempted to dereference a non-dereferenceable unordered container const_local_iterator");
706 return pointer_traits<pointer>::pointer_to(__node_->__upcast()->__value_);
707 }
708
709 _LIBCPP_INLINE_VISIBILITY
710 __hash_const_local_iterator& operator++() {
711 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
Howard Hinnantb24c8022013-07-23 22:01:58 +0000712 "Attempted to increment non-incrementable unordered container const_local_iterator");
Howard Hinnant3e519522010-05-11 19:42:16 +0000713 __node_ = __node_->__next_;
Eric Fiselier40492ba2016-07-23 20:36:55 +0000714 if (__node_ != nullptr && __constrain_hash(__node_->__hash(), __bucket_count_) != __bucket_)
Howard Hinnant3e519522010-05-11 19:42:16 +0000715 __node_ = nullptr;
716 return *this;
717 }
718
Howard Hinnant43d99232010-09-21 17:32:39 +0000719 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000720 __hash_const_local_iterator operator++(int)
721 {
722 __hash_const_local_iterator __t(*this);
723 ++(*this);
724 return __t;
725 }
726
Howard Hinnant43d99232010-09-21 17:32:39 +0000727 friend _LIBCPP_INLINE_VISIBILITY
728 bool operator==(const __hash_const_local_iterator& __x, const __hash_const_local_iterator& __y)
Howard Hinnantb24c8022013-07-23 22:01:58 +0000729 {
Howard Hinnantb24c8022013-07-23 22:01:58 +0000730 return __x.__node_ == __y.__node_;
731 }
Howard Hinnant43d99232010-09-21 17:32:39 +0000732 friend _LIBCPP_INLINE_VISIBILITY
733 bool operator!=(const __hash_const_local_iterator& __x, const __hash_const_local_iterator& __y)
Howard Hinnantb24c8022013-07-23 22:01:58 +0000734 {return !(__x == __y);}
Howard Hinnant3e519522010-05-11 19:42:16 +0000735
736private:
Howard Hinnantb24c8022013-07-23 22:01:58 +0000737#if _LIBCPP_DEBUG_LEVEL >= 2
738 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000739 __hash_const_local_iterator(__next_pointer __node, size_t __bucket,
Howard Hinnantb24c8022013-07-23 22:01:58 +0000740 size_t __bucket_count, const void* __c) _NOEXCEPT
741 : __node_(__node),
742 __bucket_(__bucket),
743 __bucket_count_(__bucket_count)
744 {
745 __get_db()->__insert_ic(this, __c);
746 if (__node_ != nullptr)
747 __node_ = __node_->__next_;
748 }
749#else
Howard Hinnant43d99232010-09-21 17:32:39 +0000750 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000751 __hash_const_local_iterator(__next_pointer __node, size_t __bucket,
Howard Hinnant37141072011-06-04 18:54:24 +0000752 size_t __bucket_count) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000753 : __node_(__node),
754 __bucket_(__bucket),
755 __bucket_count_(__bucket_count)
756 {
757 if (__node_ != nullptr)
758 __node_ = __node_->__next_;
759 }
Howard Hinnantb24c8022013-07-23 22:01:58 +0000760#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000761 template <class, class, class, class> friend class __hash_table;
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +0000762 template <class> friend class _LIBCPP_TEMPLATE_VIS __hash_map_const_iterator;
Howard Hinnant3e519522010-05-11 19:42:16 +0000763};
764
765template <class _Alloc>
766class __bucket_list_deallocator
767{
768 typedef _Alloc allocator_type;
769 typedef allocator_traits<allocator_type> __alloc_traits;
770 typedef typename __alloc_traits::size_type size_type;
771
772 __compressed_pair<size_type, allocator_type> __data_;
773public:
774 typedef typename __alloc_traits::pointer pointer;
775
Howard Hinnant43d99232010-09-21 17:32:39 +0000776 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000777 __bucket_list_deallocator()
Howard Hinnant37141072011-06-04 18:54:24 +0000778 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +0000779 : __data_(0) {}
Howard Hinnant43d99232010-09-21 17:32:39 +0000780
781 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000782 __bucket_list_deallocator(const allocator_type& __a, size_type __size)
Howard Hinnant37141072011-06-04 18:54:24 +0000783 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +0000784 : __data_(__size, __a) {}
785
Eric Fiselierafa7a952017-04-19 01:23:04 +0000786#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant43d99232010-09-21 17:32:39 +0000787 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000788 __bucket_list_deallocator(__bucket_list_deallocator&& __x)
Howard Hinnant37141072011-06-04 18:54:24 +0000789 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnantce48a112011-06-30 21:18:19 +0000790 : __data_(_VSTD::move(__x.__data_))
Howard Hinnant3e519522010-05-11 19:42:16 +0000791 {
792 __x.size() = 0;
793 }
Eric Fiselierafa7a952017-04-19 01:23:04 +0000794#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000795
Howard Hinnant37141072011-06-04 18:54:24 +0000796 _LIBCPP_INLINE_VISIBILITY
797 size_type& size() _NOEXCEPT {return __data_.first();}
798 _LIBCPP_INLINE_VISIBILITY
799 size_type size() const _NOEXCEPT {return __data_.first();}
Howard Hinnant3e519522010-05-11 19:42:16 +0000800
Howard Hinnant43d99232010-09-21 17:32:39 +0000801 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +0000802 allocator_type& __alloc() _NOEXCEPT {return __data_.second();}
803 _LIBCPP_INLINE_VISIBILITY
804 const allocator_type& __alloc() const _NOEXCEPT {return __data_.second();}
805
806 _LIBCPP_INLINE_VISIBILITY
807 void operator()(pointer __p) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000808 {
809 __alloc_traits::deallocate(__alloc(), __p, size());
810 }
811};
812
Howard Hinnantce534202011-06-14 19:58:17 +0000813template <class _Alloc> class __hash_map_node_destructor;
Howard Hinnant3e519522010-05-11 19:42:16 +0000814
815template <class _Alloc>
816class __hash_node_destructor
817{
818 typedef _Alloc allocator_type;
819 typedef allocator_traits<allocator_type> __alloc_traits;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000820
Howard Hinnant3e519522010-05-11 19:42:16 +0000821public:
822 typedef typename __alloc_traits::pointer pointer;
823private:
Eric Fiselierfcd02212016-02-11 11:59:44 +0000824 typedef __hash_node_types<pointer> _NodeTypes;
Howard Hinnant3e519522010-05-11 19:42:16 +0000825
826 allocator_type& __na_;
827
828 __hash_node_destructor& operator=(const __hash_node_destructor&);
829
830public:
831 bool __value_constructed;
832
Howard Hinnant43d99232010-09-21 17:32:39 +0000833 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf622b582011-07-31 17:04:30 +0000834 explicit __hash_node_destructor(allocator_type& __na,
835 bool __constructed = false) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000836 : __na_(__na),
Howard Hinnantf622b582011-07-31 17:04:30 +0000837 __value_constructed(__constructed)
Howard Hinnant3e519522010-05-11 19:42:16 +0000838 {}
839
Howard Hinnant43d99232010-09-21 17:32:39 +0000840 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +0000841 void operator()(pointer __p) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000842 {
843 if (__value_constructed)
Eric Fiselierfcd02212016-02-11 11:59:44 +0000844 __alloc_traits::destroy(__na_, _NodeTypes::__get_ptr(__p->__value_));
Howard Hinnant3e519522010-05-11 19:42:16 +0000845 if (__p)
846 __alloc_traits::deallocate(__na_, __p, 1);
847 }
848
849 template <class> friend class __hash_map_node_destructor;
850};
851
Erik Pilkingtonb0386a52018-08-01 01:33:38 +0000852#if _LIBCPP_STD_VER > 14
853template <class _NodeType, class _Alloc>
854struct __generic_container_node_destructor;
855
856template <class _Tp, class _VoidPtr, class _Alloc>
857struct __generic_container_node_destructor<__hash_node<_Tp, _VoidPtr>, _Alloc>
858 : __hash_node_destructor<_Alloc>
859{
860 using __hash_node_destructor<_Alloc>::__hash_node_destructor;
861};
862#endif
Eric Fiselier04333f92017-01-13 22:42:53 +0000863
Louis Dionne3560fbf32018-12-06 21:46:17 +0000864template <class _Key, class _Hash, class _Equal>
865struct __enforce_unordered_container_requirements {
Eric Fiselier04333f92017-01-13 22:42:53 +0000866#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierbd6a2d82017-03-03 22:35:58 +0000867 static_assert(__check_hash_requirements<_Key, _Hash>::value,
Louis Dionne3560fbf32018-12-06 21:46:17 +0000868 "the specified hash does not meet the Hash requirements");
Eric Fiselieracb21582017-03-01 02:02:28 +0000869 static_assert(is_copy_constructible<_Equal>::value,
Louis Dionne3560fbf32018-12-06 21:46:17 +0000870 "the specified comparator is required to be copy constructible");
871#endif
872 typedef int type;
Eric Fiselier04333f92017-01-13 22:42:53 +0000873};
874
Louis Dionne3560fbf32018-12-06 21:46:17 +0000875template <class _Key, class _Hash, class _Equal>
876#ifndef _LIBCPP_CXX03_LANG
877 _LIBCPP_DIAGNOSE_WARNING(!__invokable<_Equal const&, _Key const&, _Key const&>::value,
878 "the specified comparator type does not provide a const call operator")
879 _LIBCPP_DIAGNOSE_WARNING(!__invokable<_Hash const&, _Key const&>::value,
880 "the specified hash functor does not provide a const call operator")
881#endif
882typename __enforce_unordered_container_requirements<_Key, _Hash, _Equal>::type
883__diagnose_unordered_container_requirements(int);
884
885// This dummy overload is used so that the compiler won't emit a spurious
886// "no matching function for call to __diagnose_unordered_xxx" diagnostic
887// when the overload above causes a hard error.
888template <class _Key, class _Hash, class _Equal>
889int __diagnose_unordered_container_requirements(void*);
Eric Fiselier04333f92017-01-13 22:42:53 +0000890
Howard Hinnant3e519522010-05-11 19:42:16 +0000891template <class _Tp, class _Hash, class _Equal, class _Alloc>
892class __hash_table
893{
894public:
895 typedef _Tp value_type;
896 typedef _Hash hasher;
897 typedef _Equal key_equal;
898 typedef _Alloc allocator_type;
899
900private:
901 typedef allocator_traits<allocator_type> __alloc_traits;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000902 typedef typename
903 __make_hash_node_types<value_type, typename __alloc_traits::void_pointer>::type
904 _NodeTypes;
Howard Hinnant3e519522010-05-11 19:42:16 +0000905public:
Eric Fiselierfcd02212016-02-11 11:59:44 +0000906
907 typedef typename _NodeTypes::__node_value_type __node_value_type;
908 typedef typename _NodeTypes::__container_value_type __container_value_type;
Duncan P. N. Exon Smithfde79b42016-03-17 20:45:20 +0000909 typedef typename _NodeTypes::key_type key_type;
Howard Hinnant3e519522010-05-11 19:42:16 +0000910 typedef value_type& reference;
911 typedef const value_type& const_reference;
912 typedef typename __alloc_traits::pointer pointer;
913 typedef typename __alloc_traits::const_pointer const_pointer;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000914#ifndef _LIBCPP_ABI_FIX_UNORDERED_CONTAINER_SIZE_TYPE
Howard Hinnant3e519522010-05-11 19:42:16 +0000915 typedef typename __alloc_traits::size_type size_type;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000916#else
917 typedef typename _NodeTypes::size_type size_type;
918#endif
919 typedef typename _NodeTypes::difference_type difference_type;
Howard Hinnant3e519522010-05-11 19:42:16 +0000920public:
921 // Create __node
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000922
923 typedef typename _NodeTypes::__node_type __node;
Marshall Clow1f508012015-04-07 05:21:38 +0000924 typedef typename __rebind_alloc_helper<__alloc_traits, __node>::type __node_allocator;
Howard Hinnant3e519522010-05-11 19:42:16 +0000925 typedef allocator_traits<__node_allocator> __node_traits;
Eric Fiselier8e397682016-02-11 15:22:37 +0000926 typedef typename _NodeTypes::__void_pointer __void_pointer;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000927 typedef typename _NodeTypes::__node_pointer __node_pointer;
928 typedef typename _NodeTypes::__node_pointer __node_const_pointer;
929 typedef typename _NodeTypes::__node_base_type __first_node;
930 typedef typename _NodeTypes::__node_base_pointer __node_base_pointer;
Eric Fiselier40492ba2016-07-23 20:36:55 +0000931 typedef typename _NodeTypes::__next_pointer __next_pointer;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000932
933private:
934 // check for sane allocator pointer rebinding semantics. Rebinding the
935 // allocator for a new pointer type should be exactly the same as rebinding
936 // the pointer using 'pointer_traits'.
937 static_assert((is_same<__node_pointer, typename __node_traits::pointer>::value),
938 "Allocator does not rebind pointers in a sane manner.");
939 typedef typename __rebind_alloc_helper<__node_traits, __first_node>::type
940 __node_base_allocator;
941 typedef allocator_traits<__node_base_allocator> __node_base_traits;
942 static_assert((is_same<__node_base_pointer, typename __node_base_traits::pointer>::value),
943 "Allocator does not rebind pointers in a sane manner.");
Howard Hinnant3e519522010-05-11 19:42:16 +0000944
945private:
946
Eric Fiselier40492ba2016-07-23 20:36:55 +0000947 typedef typename __rebind_alloc_helper<__node_traits, __next_pointer>::type __pointer_allocator;
Howard Hinnant3e519522010-05-11 19:42:16 +0000948 typedef __bucket_list_deallocator<__pointer_allocator> __bucket_list_deleter;
Eric Fiselier40492ba2016-07-23 20:36:55 +0000949 typedef unique_ptr<__next_pointer[], __bucket_list_deleter> __bucket_list;
Howard Hinnant3e519522010-05-11 19:42:16 +0000950 typedef allocator_traits<__pointer_allocator> __pointer_alloc_traits;
Eric Fiselier40492ba2016-07-23 20:36:55 +0000951 typedef typename __bucket_list_deleter::pointer __node_pointer_pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000952
953 // --- Member data begin ---
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000954 __bucket_list __bucket_list_;
955 __compressed_pair<__first_node, __node_allocator> __p1_;
956 __compressed_pair<size_type, hasher> __p2_;
957 __compressed_pair<float, key_equal> __p3_;
Howard Hinnant3e519522010-05-11 19:42:16 +0000958 // --- Member data end ---
959
Howard Hinnant37141072011-06-04 18:54:24 +0000960 _LIBCPP_INLINE_VISIBILITY
961 size_type& size() _NOEXCEPT {return __p2_.first();}
Howard Hinnant3e519522010-05-11 19:42:16 +0000962public:
Howard Hinnant37141072011-06-04 18:54:24 +0000963 _LIBCPP_INLINE_VISIBILITY
964 size_type size() const _NOEXCEPT {return __p2_.first();}
Howard Hinnant3e519522010-05-11 19:42:16 +0000965
Howard Hinnant37141072011-06-04 18:54:24 +0000966 _LIBCPP_INLINE_VISIBILITY
967 hasher& hash_function() _NOEXCEPT {return __p2_.second();}
968 _LIBCPP_INLINE_VISIBILITY
969 const hasher& hash_function() const _NOEXCEPT {return __p2_.second();}
Howard Hinnant3e519522010-05-11 19:42:16 +0000970
Howard Hinnant37141072011-06-04 18:54:24 +0000971 _LIBCPP_INLINE_VISIBILITY
972 float& max_load_factor() _NOEXCEPT {return __p3_.first();}
973 _LIBCPP_INLINE_VISIBILITY
974 float max_load_factor() const _NOEXCEPT {return __p3_.first();}
Howard Hinnant3e519522010-05-11 19:42:16 +0000975
Howard Hinnant37141072011-06-04 18:54:24 +0000976 _LIBCPP_INLINE_VISIBILITY
977 key_equal& key_eq() _NOEXCEPT {return __p3_.second();}
978 _LIBCPP_INLINE_VISIBILITY
979 const key_equal& key_eq() const _NOEXCEPT {return __p3_.second();}
Howard Hinnant3e519522010-05-11 19:42:16 +0000980
Howard Hinnant37141072011-06-04 18:54:24 +0000981 _LIBCPP_INLINE_VISIBILITY
982 __node_allocator& __node_alloc() _NOEXCEPT {return __p1_.second();}
983 _LIBCPP_INLINE_VISIBILITY
984 const __node_allocator& __node_alloc() const _NOEXCEPT
985 {return __p1_.second();}
Howard Hinnant3e519522010-05-11 19:42:16 +0000986
987public:
988 typedef __hash_iterator<__node_pointer> iterator;
Howard Hinnant307f8142013-06-22 15:21:29 +0000989 typedef __hash_const_iterator<__node_pointer> const_iterator;
Howard Hinnant3e519522010-05-11 19:42:16 +0000990 typedef __hash_local_iterator<__node_pointer> local_iterator;
Howard Hinnant307f8142013-06-22 15:21:29 +0000991 typedef __hash_const_local_iterator<__node_pointer> const_local_iterator;
Howard Hinnant3e519522010-05-11 19:42:16 +0000992
Evgeniy Stepanov906c8722015-11-07 01:22:13 +0000993 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +0000994 __hash_table()
995 _NOEXCEPT_(
996 is_nothrow_default_constructible<__bucket_list>::value &&
997 is_nothrow_default_constructible<__first_node>::value &&
998 is_nothrow_default_constructible<__node_allocator>::value &&
999 is_nothrow_default_constructible<hasher>::value &&
1000 is_nothrow_default_constructible<key_equal>::value);
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001001 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001002 __hash_table(const hasher& __hf, const key_equal& __eql);
1003 __hash_table(const hasher& __hf, const key_equal& __eql,
1004 const allocator_type& __a);
1005 explicit __hash_table(const allocator_type& __a);
1006 __hash_table(const __hash_table& __u);
1007 __hash_table(const __hash_table& __u, const allocator_type& __a);
Eric Fiselierfcd02212016-02-11 11:59:44 +00001008#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant37141072011-06-04 18:54:24 +00001009 __hash_table(__hash_table&& __u)
1010 _NOEXCEPT_(
1011 is_nothrow_move_constructible<__bucket_list>::value &&
1012 is_nothrow_move_constructible<__first_node>::value &&
1013 is_nothrow_move_constructible<__node_allocator>::value &&
1014 is_nothrow_move_constructible<hasher>::value &&
1015 is_nothrow_move_constructible<key_equal>::value);
Howard Hinnant3e519522010-05-11 19:42:16 +00001016 __hash_table(__hash_table&& __u, const allocator_type& __a);
Eric Fiselierfcd02212016-02-11 11:59:44 +00001017#endif // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001018 ~__hash_table();
1019
1020 __hash_table& operator=(const __hash_table& __u);
Eric Fiselierfcd02212016-02-11 11:59:44 +00001021#ifndef _LIBCPP_CXX03_LANG
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001022 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001023 __hash_table& operator=(__hash_table&& __u)
1024 _NOEXCEPT_(
1025 __node_traits::propagate_on_container_move_assignment::value &&
1026 is_nothrow_move_assignable<__node_allocator>::value &&
1027 is_nothrow_move_assignable<hasher>::value &&
1028 is_nothrow_move_assignable<key_equal>::value);
Howard Hinnant3e519522010-05-11 19:42:16 +00001029#endif
1030 template <class _InputIterator>
1031 void __assign_unique(_InputIterator __first, _InputIterator __last);
1032 template <class _InputIterator>
1033 void __assign_multi(_InputIterator __first, _InputIterator __last);
1034
Howard Hinnant43d99232010-09-21 17:32:39 +00001035 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001036 size_type max_size() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001037 {
Eric Fiselier55b31b4e2016-11-23 01:18:56 +00001038 return std::min<size_type>(
Eric Fiselier341c9dd2016-11-23 09:16:12 +00001039 __node_traits::max_size(__node_alloc()),
Eric Fiselier55b31b4e2016-11-23 01:18:56 +00001040 numeric_limits<difference_type >::max()
1041 );
Howard Hinnant3e519522010-05-11 19:42:16 +00001042 }
1043
Erik Pilkington5c4e07a2018-10-31 17:31:35 +00001044private:
1045 _LIBCPP_INLINE_VISIBILITY
1046 __next_pointer __node_insert_multi_prepare(size_t __cp_hash,
1047 value_type& __cp_val);
1048 _LIBCPP_INLINE_VISIBILITY
1049 void __node_insert_multi_perform(__node_pointer __cp,
1050 __next_pointer __pn) _NOEXCEPT;
1051
1052 _LIBCPP_INLINE_VISIBILITY
1053 __next_pointer __node_insert_unique_prepare(size_t __nd_hash,
1054 value_type& __nd_val);
1055 _LIBCPP_INLINE_VISIBILITY
1056 void __node_insert_unique_perform(__node_pointer __ptr) _NOEXCEPT;
1057
1058public:
1059 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001060 pair<iterator, bool> __node_insert_unique(__node_pointer __nd);
Erik Pilkington5c4e07a2018-10-31 17:31:35 +00001061 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001062 iterator __node_insert_multi(__node_pointer __nd);
Erik Pilkington5c4e07a2018-10-31 17:31:35 +00001063 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001064 iterator __node_insert_multi(const_iterator __p,
1065 __node_pointer __nd);
1066
Eric Fiselierfcd02212016-02-11 11:59:44 +00001067#ifndef _LIBCPP_CXX03_LANG
1068 template <class _Key, class ..._Args>
Duncan P. N. Exon Smithfde79b42016-03-17 20:45:20 +00001069 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierfcd02212016-02-11 11:59:44 +00001070 pair<iterator, bool> __emplace_unique_key_args(_Key const& __k, _Args&&... __args);
Howard Hinnant3e519522010-05-11 19:42:16 +00001071
Eric Fiselierfcd02212016-02-11 11:59:44 +00001072 template <class... _Args>
Duncan P. N. Exon Smithfde79b42016-03-17 20:45:20 +00001073 _LIBCPP_INLINE_VISIBILITY
1074 pair<iterator, bool> __emplace_unique_impl(_Args&&... __args);
1075
1076 template <class _Pp>
1077 _LIBCPP_INLINE_VISIBILITY
1078 pair<iterator, bool> __emplace_unique(_Pp&& __x) {
1079 return __emplace_unique_extract_key(_VSTD::forward<_Pp>(__x),
1080 __can_extract_key<_Pp, key_type>());
1081 }
Eric Fiselier50088682016-04-16 00:23:12 +00001082
1083 template <class _First, class _Second>
1084 _LIBCPP_INLINE_VISIBILITY
1085 typename enable_if<
1086 __can_extract_map_key<_First, key_type, __container_value_type>::value,
1087 pair<iterator, bool>
1088 >::type __emplace_unique(_First&& __f, _Second&& __s) {
1089 return __emplace_unique_key_args(__f, _VSTD::forward<_First>(__f),
1090 _VSTD::forward<_Second>(__s));
1091 }
1092
Eric Fiselierfcd02212016-02-11 11:59:44 +00001093 template <class... _Args>
Duncan P. N. Exon Smithfde79b42016-03-17 20:45:20 +00001094 _LIBCPP_INLINE_VISIBILITY
1095 pair<iterator, bool> __emplace_unique(_Args&&... __args) {
1096 return __emplace_unique_impl(_VSTD::forward<_Args>(__args)...);
1097 }
1098
1099 template <class _Pp>
1100 _LIBCPP_INLINE_VISIBILITY
1101 pair<iterator, bool>
1102 __emplace_unique_extract_key(_Pp&& __x, __extract_key_fail_tag) {
1103 return __emplace_unique_impl(_VSTD::forward<_Pp>(__x));
1104 }
1105 template <class _Pp>
1106 _LIBCPP_INLINE_VISIBILITY
1107 pair<iterator, bool>
1108 __emplace_unique_extract_key(_Pp&& __x, __extract_key_self_tag) {
1109 return __emplace_unique_key_args(__x, _VSTD::forward<_Pp>(__x));
1110 }
1111 template <class _Pp>
1112 _LIBCPP_INLINE_VISIBILITY
1113 pair<iterator, bool>
1114 __emplace_unique_extract_key(_Pp&& __x, __extract_key_first_tag) {
1115 return __emplace_unique_key_args(__x.first, _VSTD::forward<_Pp>(__x));
1116 }
1117
1118 template <class... _Args>
1119 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierfcd02212016-02-11 11:59:44 +00001120 iterator __emplace_multi(_Args&&... __args);
1121 template <class... _Args>
Duncan P. N. Exon Smithfde79b42016-03-17 20:45:20 +00001122 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierfcd02212016-02-11 11:59:44 +00001123 iterator __emplace_hint_multi(const_iterator __p, _Args&&... __args);
1124
1125
Eric Fiselierde3f2b32015-06-13 07:18:32 +00001126 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierfcd02212016-02-11 11:59:44 +00001127 pair<iterator, bool>
1128 __insert_unique(__container_value_type&& __x) {
1129 return __emplace_unique_key_args(_NodeTypes::__get_key(__x), _VSTD::move(__x));
1130 }
1131
1132 template <class _Pp, class = typename enable_if<
1133 !__is_same_uncvref<_Pp, __container_value_type>::value
1134 >::type>
Eric Fiselierde3f2b32015-06-13 07:18:32 +00001135 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierfcd02212016-02-11 11:59:44 +00001136 pair<iterator, bool> __insert_unique(_Pp&& __x) {
1137 return __emplace_unique(_VSTD::forward<_Pp>(__x));
1138 }
1139
1140 template <class _Pp>
1141 _LIBCPP_INLINE_VISIBILITY
1142 iterator __insert_multi(_Pp&& __x) {
1143 return __emplace_multi(_VSTD::forward<_Pp>(__x));
1144 }
1145
1146 template <class _Pp>
1147 _LIBCPP_INLINE_VISIBILITY
1148 iterator __insert_multi(const_iterator __p, _Pp&& __x) {
1149 return __emplace_hint_multi(__p, _VSTD::forward<_Pp>(__x));
1150 }
1151
1152#else // !defined(_LIBCPP_CXX03_LANG)
1153 template <class _Key, class _Args>
Eric Fiselier4271d012016-09-25 04:05:46 +00001154 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierfcd02212016-02-11 11:59:44 +00001155 pair<iterator, bool> __emplace_unique_key_args(_Key const&, _Args& __args);
1156
1157 iterator __insert_multi(const __container_value_type& __x);
1158 iterator __insert_multi(const_iterator __p, const __container_value_type& __x);
Eric Fiselierde3f2b32015-06-13 07:18:32 +00001159#endif
1160
Eric Fiselierfcd02212016-02-11 11:59:44 +00001161 _LIBCPP_INLINE_VISIBILITY
1162 pair<iterator, bool> __insert_unique(const __container_value_type& __x) {
1163 return __emplace_unique_key_args(_NodeTypes::__get_key(__x), __x);
1164 }
Howard Hinnant3e519522010-05-11 19:42:16 +00001165
Erik Pilkingtonb0386a52018-08-01 01:33:38 +00001166#if _LIBCPP_STD_VER > 14
1167 template <class _NodeHandle, class _InsertReturnType>
1168 _LIBCPP_INLINE_VISIBILITY
1169 _InsertReturnType __node_handle_insert_unique(_NodeHandle&& __nh);
1170 template <class _NodeHandle>
1171 _LIBCPP_INLINE_VISIBILITY
1172 iterator __node_handle_insert_unique(const_iterator __hint,
1173 _NodeHandle&& __nh);
Erik Pilkington5c4e07a2018-10-31 17:31:35 +00001174 template <class _Table>
1175 _LIBCPP_INLINE_VISIBILITY
1176 void __node_handle_merge_unique(_Table& __source);
Erik Pilkingtonb0386a52018-08-01 01:33:38 +00001177
1178 template <class _NodeHandle>
1179 _LIBCPP_INLINE_VISIBILITY
1180 iterator __node_handle_insert_multi(_NodeHandle&& __nh);
1181 template <class _NodeHandle>
1182 _LIBCPP_INLINE_VISIBILITY
1183 iterator __node_handle_insert_multi(const_iterator __hint, _NodeHandle&& __nh);
Erik Pilkington5c4e07a2018-10-31 17:31:35 +00001184 template <class _Table>
1185 _LIBCPP_INLINE_VISIBILITY
1186 void __node_handle_merge_multi(_Table& __source);
Erik Pilkingtonb0386a52018-08-01 01:33:38 +00001187
1188 template <class _NodeHandle>
1189 _LIBCPP_INLINE_VISIBILITY
1190 _NodeHandle __node_handle_extract(key_type const& __key);
1191 template <class _NodeHandle>
1192 _LIBCPP_INLINE_VISIBILITY
1193 _NodeHandle __node_handle_extract(const_iterator __it);
1194#endif
1195
Howard Hinnant37141072011-06-04 18:54:24 +00001196 void clear() _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +00001197 void rehash(size_type __n);
Howard Hinnant43d99232010-09-21 17:32:39 +00001198 _LIBCPP_INLINE_VISIBILITY void reserve(size_type __n)
Howard Hinnant3e519522010-05-11 19:42:16 +00001199 {rehash(static_cast<size_type>(ceil(__n / max_load_factor())));}
Howard Hinnant43d99232010-09-21 17:32:39 +00001200
1201 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001202 size_type bucket_count() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001203 {
1204 return __bucket_list_.get_deleter().size();
1205 }
1206
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001207 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001208 iterator begin() _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001209 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001210 iterator end() _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001211 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001212 const_iterator begin() const _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001213 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001214 const_iterator end() const _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +00001215
1216 template <class _Key>
Howard Hinnant43d99232010-09-21 17:32:39 +00001217 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001218 size_type bucket(const _Key& __k) const
Howard Hinnante5c13de2013-07-29 19:05:47 +00001219 {
1220 _LIBCPP_ASSERT(bucket_count() > 0,
1221 "unordered container::bucket(key) called when bucket_count() == 0");
1222 return __constrain_hash(hash_function()(__k), bucket_count());
1223 }
Howard Hinnant3e519522010-05-11 19:42:16 +00001224
1225 template <class _Key>
1226 iterator find(const _Key& __x);
1227 template <class _Key>
1228 const_iterator find(const _Key& __x) const;
1229
Howard Hinnantc003db12011-11-29 18:15:50 +00001230 typedef __hash_node_destructor<__node_allocator> _Dp;
1231 typedef unique_ptr<__node, _Dp> __node_holder;
Howard Hinnant3e519522010-05-11 19:42:16 +00001232
1233 iterator erase(const_iterator __p);
1234 iterator erase(const_iterator __first, const_iterator __last);
1235 template <class _Key>
1236 size_type __erase_unique(const _Key& __k);
1237 template <class _Key>
1238 size_type __erase_multi(const _Key& __k);
Howard Hinnant37141072011-06-04 18:54:24 +00001239 __node_holder remove(const_iterator __p) _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +00001240
1241 template <class _Key>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001242 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001243 size_type __count_unique(const _Key& __k) const;
1244 template <class _Key>
1245 size_type __count_multi(const _Key& __k) const;
1246
1247 template <class _Key>
1248 pair<iterator, iterator>
1249 __equal_range_unique(const _Key& __k);
1250 template <class _Key>
1251 pair<const_iterator, const_iterator>
1252 __equal_range_unique(const _Key& __k) const;
1253
1254 template <class _Key>
1255 pair<iterator, iterator>
1256 __equal_range_multi(const _Key& __k);
1257 template <class _Key>
1258 pair<const_iterator, const_iterator>
1259 __equal_range_multi(const _Key& __k) const;
1260
Howard Hinnant37141072011-06-04 18:54:24 +00001261 void swap(__hash_table& __u)
Eric Fiselier87a82492015-07-18 20:40:46 +00001262#if _LIBCPP_STD_VER <= 11
Eric Fiselier780b51d2016-12-28 05:53:01 +00001263 _NOEXCEPT_DEBUG_(
Marshall Clowe3fbe142015-07-13 20:04:56 +00001264 __is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value
Marshall Clowe3fbe142015-07-13 20:04:56 +00001265 && (!allocator_traits<__pointer_allocator>::propagate_on_container_swap::value
1266 || __is_nothrow_swappable<__pointer_allocator>::value)
1267 && (!__node_traits::propagate_on_container_swap::value
1268 || __is_nothrow_swappable<__node_allocator>::value)
Marshall Clowe3fbe142015-07-13 20:04:56 +00001269 );
Eric Fiselier87a82492015-07-18 20:40:46 +00001270#else
Eric Fiselier780b51d2016-12-28 05:53:01 +00001271 _NOEXCEPT_DEBUG_(__is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value);
Eric Fiselier87a82492015-07-18 20:40:46 +00001272#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001273
Howard Hinnant43d99232010-09-21 17:32:39 +00001274 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001275 size_type max_bucket_count() const _NOEXCEPT
Eric Fiselier55b31b4e2016-11-23 01:18:56 +00001276 {return max_size(); }
Howard Hinnant3e519522010-05-11 19:42:16 +00001277 size_type bucket_size(size_type __n) const;
Howard Hinnant37141072011-06-04 18:54:24 +00001278 _LIBCPP_INLINE_VISIBILITY float load_factor() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001279 {
1280 size_type __bc = bucket_count();
1281 return __bc != 0 ? (float)size() / __bc : 0.f;
1282 }
Howard Hinnant37141072011-06-04 18:54:24 +00001283 _LIBCPP_INLINE_VISIBILITY void max_load_factor(float __mlf) _NOEXCEPT
Howard Hinnante5c13de2013-07-29 19:05:47 +00001284 {
1285 _LIBCPP_ASSERT(__mlf > 0,
1286 "unordered container::max_load_factor(lf) called with lf <= 0");
1287 max_load_factor() = _VSTD::max(__mlf, load_factor());
1288 }
Howard Hinnant3e519522010-05-11 19:42:16 +00001289
Howard Hinnantb24c8022013-07-23 22:01:58 +00001290 _LIBCPP_INLINE_VISIBILITY
1291 local_iterator
1292 begin(size_type __n)
1293 {
Howard Hinnante5c13de2013-07-29 19:05:47 +00001294 _LIBCPP_ASSERT(__n < bucket_count(),
1295 "unordered container::begin(n) called with n >= bucket_count()");
Howard Hinnantb24c8022013-07-23 22:01:58 +00001296#if _LIBCPP_DEBUG_LEVEL >= 2
1297 return local_iterator(__bucket_list_[__n], __n, bucket_count(), this);
1298#else
1299 return local_iterator(__bucket_list_[__n], __n, bucket_count());
1300#endif
1301 }
1302
1303 _LIBCPP_INLINE_VISIBILITY
1304 local_iterator
1305 end(size_type __n)
1306 {
Howard Hinnante5c13de2013-07-29 19:05:47 +00001307 _LIBCPP_ASSERT(__n < bucket_count(),
1308 "unordered container::end(n) called with n >= bucket_count()");
Howard Hinnantb24c8022013-07-23 22:01:58 +00001309#if _LIBCPP_DEBUG_LEVEL >= 2
1310 return local_iterator(nullptr, __n, bucket_count(), this);
1311#else
1312 return local_iterator(nullptr, __n, bucket_count());
1313#endif
1314 }
1315
1316 _LIBCPP_INLINE_VISIBILITY
1317 const_local_iterator
1318 cbegin(size_type __n) const
1319 {
Howard Hinnante5c13de2013-07-29 19:05:47 +00001320 _LIBCPP_ASSERT(__n < bucket_count(),
1321 "unordered container::cbegin(n) called with n >= bucket_count()");
Howard Hinnantb24c8022013-07-23 22:01:58 +00001322#if _LIBCPP_DEBUG_LEVEL >= 2
1323 return const_local_iterator(__bucket_list_[__n], __n, bucket_count(), this);
1324#else
1325 return const_local_iterator(__bucket_list_[__n], __n, bucket_count());
1326#endif
1327 }
1328
1329 _LIBCPP_INLINE_VISIBILITY
1330 const_local_iterator
1331 cend(size_type __n) const
1332 {
Howard Hinnante5c13de2013-07-29 19:05:47 +00001333 _LIBCPP_ASSERT(__n < bucket_count(),
1334 "unordered container::cend(n) called with n >= bucket_count()");
Howard Hinnantb24c8022013-07-23 22:01:58 +00001335#if _LIBCPP_DEBUG_LEVEL >= 2
1336 return const_local_iterator(nullptr, __n, bucket_count(), this);
1337#else
1338 return const_local_iterator(nullptr, __n, bucket_count());
1339#endif
1340 }
1341
1342#if _LIBCPP_DEBUG_LEVEL >= 2
1343
1344 bool __dereferenceable(const const_iterator* __i) const;
1345 bool __decrementable(const const_iterator* __i) const;
1346 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1347 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1348
1349#endif // _LIBCPP_DEBUG_LEVEL >= 2
1350
Howard Hinnant3e519522010-05-11 19:42:16 +00001351private:
1352 void __rehash(size_type __n);
1353
Eric Fiselierfcd02212016-02-11 11:59:44 +00001354#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001355 template <class ..._Args>
Eric Fiselierfcd02212016-02-11 11:59:44 +00001356 __node_holder __construct_node(_Args&& ...__args);
1357
1358 template <class _First, class ..._Rest>
1359 __node_holder __construct_node_hash(size_t __hash, _First&& __f, _Rest&&... __rest);
1360#else // _LIBCPP_CXX03_LANG
1361 __node_holder __construct_node(const __container_value_type& __v);
1362 __node_holder __construct_node_hash(size_t __hash, const __container_value_type& __v);
Howard Hinnant3e519522010-05-11 19:42:16 +00001363#endif
Eric Fiselierfcd02212016-02-11 11:59:44 +00001364
Howard Hinnant3e519522010-05-11 19:42:16 +00001365
Howard Hinnant43d99232010-09-21 17:32:39 +00001366 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001367 void __copy_assign_alloc(const __hash_table& __u)
1368 {__copy_assign_alloc(__u, integral_constant<bool,
1369 __node_traits::propagate_on_container_copy_assignment::value>());}
1370 void __copy_assign_alloc(const __hash_table& __u, true_type);
Howard Hinnant43d99232010-09-21 17:32:39 +00001371 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2063662011-12-01 20:21:04 +00001372 void __copy_assign_alloc(const __hash_table&, false_type) {}
Howard Hinnant3e519522010-05-11 19:42:16 +00001373
Eric Fiselierfcd02212016-02-11 11:59:44 +00001374#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001375 void __move_assign(__hash_table& __u, false_type);
Howard Hinnant37141072011-06-04 18:54:24 +00001376 void __move_assign(__hash_table& __u, true_type)
1377 _NOEXCEPT_(
1378 is_nothrow_move_assignable<__node_allocator>::value &&
1379 is_nothrow_move_assignable<hasher>::value &&
1380 is_nothrow_move_assignable<key_equal>::value);
1381 _LIBCPP_INLINE_VISIBILITY
1382 void __move_assign_alloc(__hash_table& __u)
1383 _NOEXCEPT_(
1384 !__node_traits::propagate_on_container_move_assignment::value ||
1385 (is_nothrow_move_assignable<__pointer_allocator>::value &&
1386 is_nothrow_move_assignable<__node_allocator>::value))
Howard Hinnant3e519522010-05-11 19:42:16 +00001387 {__move_assign_alloc(__u, integral_constant<bool,
1388 __node_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnant43d99232010-09-21 17:32:39 +00001389 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001390 void __move_assign_alloc(__hash_table& __u, true_type)
Howard Hinnant37141072011-06-04 18:54:24 +00001391 _NOEXCEPT_(
1392 is_nothrow_move_assignable<__pointer_allocator>::value &&
1393 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +00001394 {
1395 __bucket_list_.get_deleter().__alloc() =
Howard Hinnantce48a112011-06-30 21:18:19 +00001396 _VSTD::move(__u.__bucket_list_.get_deleter().__alloc());
1397 __node_alloc() = _VSTD::move(__u.__node_alloc());
Howard Hinnant3e519522010-05-11 19:42:16 +00001398 }
Howard Hinnant43d99232010-09-21 17:32:39 +00001399 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001400 void __move_assign_alloc(__hash_table&, false_type) _NOEXCEPT {}
Eric Fiselierfcd02212016-02-11 11:59:44 +00001401#endif // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001402
Eric Fiseliercd71f442017-01-07 03:01:24 +00001403 void __deallocate_node(__next_pointer __np) _NOEXCEPT;
Eric Fiselier40492ba2016-07-23 20:36:55 +00001404 __next_pointer __detach() _NOEXCEPT;
Howard Hinnant307f8142013-06-22 15:21:29 +00001405
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +00001406 template <class, class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS unordered_map;
1407 template <class, class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS unordered_multimap;
Howard Hinnant3e519522010-05-11 19:42:16 +00001408};
1409
1410template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001411inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001412__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table()
Howard Hinnant37141072011-06-04 18:54:24 +00001413 _NOEXCEPT_(
1414 is_nothrow_default_constructible<__bucket_list>::value &&
1415 is_nothrow_default_constructible<__first_node>::value &&
Eric Fiseliere2e332a2015-12-16 00:53:04 +00001416 is_nothrow_default_constructible<__node_allocator>::value &&
Howard Hinnant37141072011-06-04 18:54:24 +00001417 is_nothrow_default_constructible<hasher>::value &&
1418 is_nothrow_default_constructible<key_equal>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +00001419 : __p2_(0),
1420 __p3_(1.0f)
1421{
1422}
1423
1424template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001425inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001426__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const hasher& __hf,
1427 const key_equal& __eql)
1428 : __bucket_list_(nullptr, __bucket_list_deleter()),
1429 __p1_(),
1430 __p2_(0, __hf),
1431 __p3_(1.0f, __eql)
1432{
1433}
1434
1435template <class _Tp, class _Hash, class _Equal, class _Alloc>
1436__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const hasher& __hf,
1437 const key_equal& __eql,
1438 const allocator_type& __a)
1439 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
Eric Fiselierc88580c2017-04-12 23:45:53 +00001440 __p1_(__second_tag(), __node_allocator(__a)),
Howard Hinnant3e519522010-05-11 19:42:16 +00001441 __p2_(0, __hf),
1442 __p3_(1.0f, __eql)
1443{
1444}
1445
1446template <class _Tp, class _Hash, class _Equal, class _Alloc>
1447__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const allocator_type& __a)
1448 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
Eric Fiselierc88580c2017-04-12 23:45:53 +00001449 __p1_(__second_tag(), __node_allocator(__a)),
Howard Hinnant3e519522010-05-11 19:42:16 +00001450 __p2_(0),
1451 __p3_(1.0f)
1452{
1453}
1454
1455template <class _Tp, class _Hash, class _Equal, class _Alloc>
1456__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const __hash_table& __u)
1457 : __bucket_list_(nullptr,
1458 __bucket_list_deleter(allocator_traits<__pointer_allocator>::
1459 select_on_container_copy_construction(
1460 __u.__bucket_list_.get_deleter().__alloc()), 0)),
Eric Fiselierc88580c2017-04-12 23:45:53 +00001461 __p1_(__second_tag(), allocator_traits<__node_allocator>::
Howard Hinnant3e519522010-05-11 19:42:16 +00001462 select_on_container_copy_construction(__u.__node_alloc())),
1463 __p2_(0, __u.hash_function()),
1464 __p3_(__u.__p3_)
1465{
1466}
1467
1468template <class _Tp, class _Hash, class _Equal, class _Alloc>
1469__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const __hash_table& __u,
1470 const allocator_type& __a)
1471 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
Eric Fiselierc88580c2017-04-12 23:45:53 +00001472 __p1_(__second_tag(), __node_allocator(__a)),
Howard Hinnant3e519522010-05-11 19:42:16 +00001473 __p2_(0, __u.hash_function()),
1474 __p3_(__u.__p3_)
1475{
1476}
1477
Eric Fiselierfcd02212016-02-11 11:59:44 +00001478#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001479
1480template <class _Tp, class _Hash, class _Equal, class _Alloc>
1481__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(__hash_table&& __u)
Howard Hinnant37141072011-06-04 18:54:24 +00001482 _NOEXCEPT_(
1483 is_nothrow_move_constructible<__bucket_list>::value &&
1484 is_nothrow_move_constructible<__first_node>::value &&
Eric Fiseliere2e332a2015-12-16 00:53:04 +00001485 is_nothrow_move_constructible<__node_allocator>::value &&
Howard Hinnant37141072011-06-04 18:54:24 +00001486 is_nothrow_move_constructible<hasher>::value &&
1487 is_nothrow_move_constructible<key_equal>::value)
Howard Hinnantce48a112011-06-30 21:18:19 +00001488 : __bucket_list_(_VSTD::move(__u.__bucket_list_)),
1489 __p1_(_VSTD::move(__u.__p1_)),
1490 __p2_(_VSTD::move(__u.__p2_)),
1491 __p3_(_VSTD::move(__u.__p3_))
Howard Hinnant3e519522010-05-11 19:42:16 +00001492{
1493 if (size() > 0)
1494 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001495 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash(), bucket_count())] =
1496 __p1_.first().__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001497 __u.__p1_.first().__next_ = nullptr;
1498 __u.size() = 0;
1499 }
1500}
1501
1502template <class _Tp, class _Hash, class _Equal, class _Alloc>
1503__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(__hash_table&& __u,
1504 const allocator_type& __a)
1505 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
Eric Fiselierc88580c2017-04-12 23:45:53 +00001506 __p1_(__second_tag(), __node_allocator(__a)),
Howard Hinnantce48a112011-06-30 21:18:19 +00001507 __p2_(0, _VSTD::move(__u.hash_function())),
1508 __p3_(_VSTD::move(__u.__p3_))
Howard Hinnant3e519522010-05-11 19:42:16 +00001509{
1510 if (__a == allocator_type(__u.__node_alloc()))
1511 {
1512 __bucket_list_.reset(__u.__bucket_list_.release());
1513 __bucket_list_.get_deleter().size() = __u.__bucket_list_.get_deleter().size();
1514 __u.__bucket_list_.get_deleter().size() = 0;
1515 if (__u.size() > 0)
1516 {
1517 __p1_.first().__next_ = __u.__p1_.first().__next_;
1518 __u.__p1_.first().__next_ = nullptr;
Eric Fiselier40492ba2016-07-23 20:36:55 +00001519 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash(), bucket_count())] =
1520 __p1_.first().__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001521 size() = __u.size();
1522 __u.size() = 0;
1523 }
1524 }
1525}
1526
Eric Fiselierfcd02212016-02-11 11:59:44 +00001527#endif // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001528
1529template <class _Tp, class _Hash, class _Equal, class _Alloc>
1530__hash_table<_Tp, _Hash, _Equal, _Alloc>::~__hash_table()
1531{
Eric Fiselieracb21582017-03-01 02:02:28 +00001532#if defined(_LIBCPP_CXX03_LANG)
Marshall Clow3b8669e2016-06-30 22:05:45 +00001533 static_assert((is_copy_constructible<key_equal>::value),
1534 "Predicate must be copy-constructible.");
1535 static_assert((is_copy_constructible<hasher>::value),
1536 "Hasher must be copy-constructible.");
Eric Fiselier04333f92017-01-13 22:42:53 +00001537#endif
Eric Fiselieracb21582017-03-01 02:02:28 +00001538
Eric Fiseliercd71f442017-01-07 03:01:24 +00001539 __deallocate_node(__p1_.first().__next_);
Howard Hinnantb24c8022013-07-23 22:01:58 +00001540#if _LIBCPP_DEBUG_LEVEL >= 2
1541 __get_db()->__erase_c(this);
1542#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001543}
1544
1545template <class _Tp, class _Hash, class _Equal, class _Alloc>
1546void
1547__hash_table<_Tp, _Hash, _Equal, _Alloc>::__copy_assign_alloc(
1548 const __hash_table& __u, true_type)
1549{
1550 if (__node_alloc() != __u.__node_alloc())
1551 {
1552 clear();
1553 __bucket_list_.reset();
1554 __bucket_list_.get_deleter().size() = 0;
1555 }
1556 __bucket_list_.get_deleter().__alloc() = __u.__bucket_list_.get_deleter().__alloc();
1557 __node_alloc() = __u.__node_alloc();
1558}
1559
1560template <class _Tp, class _Hash, class _Equal, class _Alloc>
1561__hash_table<_Tp, _Hash, _Equal, _Alloc>&
1562__hash_table<_Tp, _Hash, _Equal, _Alloc>::operator=(const __hash_table& __u)
1563{
1564 if (this != &__u)
1565 {
1566 __copy_assign_alloc(__u);
1567 hash_function() = __u.hash_function();
1568 key_eq() = __u.key_eq();
1569 max_load_factor() = __u.max_load_factor();
1570 __assign_multi(__u.begin(), __u.end());
1571 }
1572 return *this;
1573}
1574
1575template <class _Tp, class _Hash, class _Equal, class _Alloc>
1576void
Eric Fiseliercd71f442017-01-07 03:01:24 +00001577__hash_table<_Tp, _Hash, _Equal, _Alloc>::__deallocate_node(__next_pointer __np)
Howard Hinnant37141072011-06-04 18:54:24 +00001578 _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001579{
1580 __node_allocator& __na = __node_alloc();
1581 while (__np != nullptr)
1582 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001583 __next_pointer __next = __np->__next_;
Howard Hinnantb24c8022013-07-23 22:01:58 +00001584#if _LIBCPP_DEBUG_LEVEL >= 2
1585 __c_node* __c = __get_db()->__find_c_and_lock(this);
1586 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1587 {
1588 --__p;
1589 iterator* __i = static_cast<iterator*>((*__p)->__i_);
1590 if (__i->__node_ == __np)
1591 {
1592 (*__p)->__c_ = nullptr;
1593 if (--__c->end_ != __p)
1594 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1595 }
1596 }
1597 __get_db()->unlock();
1598#endif
Eric Fiselier40492ba2016-07-23 20:36:55 +00001599 __node_pointer __real_np = __np->__upcast();
1600 __node_traits::destroy(__na, _NodeTypes::__get_ptr(__real_np->__value_));
1601 __node_traits::deallocate(__na, __real_np, 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001602 __np = __next;
1603 }
1604}
1605
1606template <class _Tp, class _Hash, class _Equal, class _Alloc>
Eric Fiselier40492ba2016-07-23 20:36:55 +00001607typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__next_pointer
Howard Hinnant37141072011-06-04 18:54:24 +00001608__hash_table<_Tp, _Hash, _Equal, _Alloc>::__detach() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001609{
1610 size_type __bc = bucket_count();
1611 for (size_type __i = 0; __i < __bc; ++__i)
1612 __bucket_list_[__i] = nullptr;
1613 size() = 0;
Eric Fiselier40492ba2016-07-23 20:36:55 +00001614 __next_pointer __cache = __p1_.first().__next_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001615 __p1_.first().__next_ = nullptr;
1616 return __cache;
1617}
1618
Eric Fiselierfcd02212016-02-11 11:59:44 +00001619#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001620
1621template <class _Tp, class _Hash, class _Equal, class _Alloc>
1622void
1623__hash_table<_Tp, _Hash, _Equal, _Alloc>::__move_assign(
1624 __hash_table& __u, true_type)
Howard Hinnant37141072011-06-04 18:54:24 +00001625 _NOEXCEPT_(
1626 is_nothrow_move_assignable<__node_allocator>::value &&
1627 is_nothrow_move_assignable<hasher>::value &&
1628 is_nothrow_move_assignable<key_equal>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +00001629{
1630 clear();
1631 __bucket_list_.reset(__u.__bucket_list_.release());
1632 __bucket_list_.get_deleter().size() = __u.__bucket_list_.get_deleter().size();
1633 __u.__bucket_list_.get_deleter().size() = 0;
1634 __move_assign_alloc(__u);
1635 size() = __u.size();
Howard Hinnantce48a112011-06-30 21:18:19 +00001636 hash_function() = _VSTD::move(__u.hash_function());
Howard Hinnant3e519522010-05-11 19:42:16 +00001637 max_load_factor() = __u.max_load_factor();
Howard Hinnantce48a112011-06-30 21:18:19 +00001638 key_eq() = _VSTD::move(__u.key_eq());
Howard Hinnant3e519522010-05-11 19:42:16 +00001639 __p1_.first().__next_ = __u.__p1_.first().__next_;
1640 if (size() > 0)
1641 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001642 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash(), bucket_count())] =
1643 __p1_.first().__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001644 __u.__p1_.first().__next_ = nullptr;
1645 __u.size() = 0;
1646 }
Howard Hinnantb24c8022013-07-23 22:01:58 +00001647#if _LIBCPP_DEBUG_LEVEL >= 2
1648 __get_db()->swap(this, &__u);
1649#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001650}
1651
1652template <class _Tp, class _Hash, class _Equal, class _Alloc>
1653void
1654__hash_table<_Tp, _Hash, _Equal, _Alloc>::__move_assign(
1655 __hash_table& __u, false_type)
1656{
1657 if (__node_alloc() == __u.__node_alloc())
1658 __move_assign(__u, true_type());
1659 else
1660 {
Howard Hinnantce48a112011-06-30 21:18:19 +00001661 hash_function() = _VSTD::move(__u.hash_function());
1662 key_eq() = _VSTD::move(__u.key_eq());
Howard Hinnant3e519522010-05-11 19:42:16 +00001663 max_load_factor() = __u.max_load_factor();
1664 if (bucket_count() != 0)
1665 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001666 __next_pointer __cache = __detach();
Howard Hinnant3e519522010-05-11 19:42:16 +00001667#ifndef _LIBCPP_NO_EXCEPTIONS
1668 try
1669 {
Howard Hinnantb3371f62010-08-22 00:02:43 +00001670#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001671 const_iterator __i = __u.begin();
1672 while (__cache != nullptr && __u.size() != 0)
1673 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001674 __cache->__upcast()->__value_ =
1675 _VSTD::move(__u.remove(__i++)->__value_);
1676 __next_pointer __next = __cache->__next_;
1677 __node_insert_multi(__cache->__upcast());
Howard Hinnant3e519522010-05-11 19:42:16 +00001678 __cache = __next;
1679 }
1680#ifndef _LIBCPP_NO_EXCEPTIONS
1681 }
1682 catch (...)
1683 {
Eric Fiseliercd71f442017-01-07 03:01:24 +00001684 __deallocate_node(__cache);
Howard Hinnant3e519522010-05-11 19:42:16 +00001685 throw;
1686 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00001687#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiseliercd71f442017-01-07 03:01:24 +00001688 __deallocate_node(__cache);
Howard Hinnant3e519522010-05-11 19:42:16 +00001689 }
1690 const_iterator __i = __u.begin();
1691 while (__u.size() != 0)
1692 {
Eric Fiselierfcd02212016-02-11 11:59:44 +00001693 __node_holder __h = __construct_node(_NodeTypes::__move(__u.remove(__i++)->__value_));
Howard Hinnant3e519522010-05-11 19:42:16 +00001694 __node_insert_multi(__h.get());
1695 __h.release();
1696 }
1697 }
1698}
1699
1700template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001701inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001702__hash_table<_Tp, _Hash, _Equal, _Alloc>&
1703__hash_table<_Tp, _Hash, _Equal, _Alloc>::operator=(__hash_table&& __u)
Howard Hinnant37141072011-06-04 18:54:24 +00001704 _NOEXCEPT_(
1705 __node_traits::propagate_on_container_move_assignment::value &&
1706 is_nothrow_move_assignable<__node_allocator>::value &&
1707 is_nothrow_move_assignable<hasher>::value &&
1708 is_nothrow_move_assignable<key_equal>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +00001709{
1710 __move_assign(__u, integral_constant<bool,
1711 __node_traits::propagate_on_container_move_assignment::value>());
1712 return *this;
1713}
1714
Eric Fiselierfcd02212016-02-11 11:59:44 +00001715#endif // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001716
1717template <class _Tp, class _Hash, class _Equal, class _Alloc>
1718template <class _InputIterator>
1719void
1720__hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_unique(_InputIterator __first,
1721 _InputIterator __last)
1722{
Eric Fiselierfcd02212016-02-11 11:59:44 +00001723 typedef iterator_traits<_InputIterator> _ITraits;
1724 typedef typename _ITraits::value_type _ItValueType;
1725 static_assert((is_same<_ItValueType, __container_value_type>::value),
1726 "__assign_unique may only be called with the containers value type");
1727
Howard Hinnant3e519522010-05-11 19:42:16 +00001728 if (bucket_count() != 0)
1729 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001730 __next_pointer __cache = __detach();
Howard Hinnant3e519522010-05-11 19:42:16 +00001731#ifndef _LIBCPP_NO_EXCEPTIONS
1732 try
1733 {
Howard Hinnantb3371f62010-08-22 00:02:43 +00001734#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001735 for (; __cache != nullptr && __first != __last; ++__first)
1736 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001737 __cache->__upcast()->__value_ = *__first;
1738 __next_pointer __next = __cache->__next_;
1739 __node_insert_unique(__cache->__upcast());
Howard Hinnant3e519522010-05-11 19:42:16 +00001740 __cache = __next;
1741 }
1742#ifndef _LIBCPP_NO_EXCEPTIONS
1743 }
1744 catch (...)
1745 {
Eric Fiseliercd71f442017-01-07 03:01:24 +00001746 __deallocate_node(__cache);
Howard Hinnant3e519522010-05-11 19:42:16 +00001747 throw;
1748 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00001749#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiseliercd71f442017-01-07 03:01:24 +00001750 __deallocate_node(__cache);
Howard Hinnant3e519522010-05-11 19:42:16 +00001751 }
1752 for (; __first != __last; ++__first)
1753 __insert_unique(*__first);
1754}
1755
1756template <class _Tp, class _Hash, class _Equal, class _Alloc>
1757template <class _InputIterator>
1758void
1759__hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_multi(_InputIterator __first,
1760 _InputIterator __last)
1761{
Eric Fiselierfcd02212016-02-11 11:59:44 +00001762 typedef iterator_traits<_InputIterator> _ITraits;
1763 typedef typename _ITraits::value_type _ItValueType;
1764 static_assert((is_same<_ItValueType, __container_value_type>::value ||
1765 is_same<_ItValueType, __node_value_type>::value),
1766 "__assign_multi may only be called with the containers value type"
1767 " or the nodes value type");
Howard Hinnant3e519522010-05-11 19:42:16 +00001768 if (bucket_count() != 0)
1769 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001770 __next_pointer __cache = __detach();
Howard Hinnant3e519522010-05-11 19:42:16 +00001771#ifndef _LIBCPP_NO_EXCEPTIONS
1772 try
1773 {
Howard Hinnantb3371f62010-08-22 00:02:43 +00001774#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001775 for (; __cache != nullptr && __first != __last; ++__first)
1776 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001777 __cache->__upcast()->__value_ = *__first;
1778 __next_pointer __next = __cache->__next_;
1779 __node_insert_multi(__cache->__upcast());
Howard Hinnant3e519522010-05-11 19:42:16 +00001780 __cache = __next;
1781 }
1782#ifndef _LIBCPP_NO_EXCEPTIONS
1783 }
1784 catch (...)
1785 {
Eric Fiseliercd71f442017-01-07 03:01:24 +00001786 __deallocate_node(__cache);
Howard Hinnant3e519522010-05-11 19:42:16 +00001787 throw;
1788 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00001789#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiseliercd71f442017-01-07 03:01:24 +00001790 __deallocate_node(__cache);
Howard Hinnant3e519522010-05-11 19:42:16 +00001791 }
1792 for (; __first != __last; ++__first)
Eric Fiselierfcd02212016-02-11 11:59:44 +00001793 __insert_multi(_NodeTypes::__get_value(*__first));
Howard Hinnant3e519522010-05-11 19:42:16 +00001794}
1795
1796template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001797inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001798typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
Howard Hinnant37141072011-06-04 18:54:24 +00001799__hash_table<_Tp, _Hash, _Equal, _Alloc>::begin() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001800{
Howard Hinnantb24c8022013-07-23 22:01:58 +00001801#if _LIBCPP_DEBUG_LEVEL >= 2
1802 return iterator(__p1_.first().__next_, this);
1803#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001804 return iterator(__p1_.first().__next_);
Howard Hinnantb24c8022013-07-23 22:01:58 +00001805#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001806}
1807
1808template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001809inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001810typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
Howard Hinnant37141072011-06-04 18:54:24 +00001811__hash_table<_Tp, _Hash, _Equal, _Alloc>::end() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001812{
Howard Hinnantb24c8022013-07-23 22:01:58 +00001813#if _LIBCPP_DEBUG_LEVEL >= 2
1814 return iterator(nullptr, this);
1815#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001816 return iterator(nullptr);
Howard Hinnantb24c8022013-07-23 22:01:58 +00001817#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001818}
1819
1820template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001821inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001822typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
Howard Hinnant37141072011-06-04 18:54:24 +00001823__hash_table<_Tp, _Hash, _Equal, _Alloc>::begin() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001824{
Howard Hinnantb24c8022013-07-23 22:01:58 +00001825#if _LIBCPP_DEBUG_LEVEL >= 2
1826 return const_iterator(__p1_.first().__next_, this);
1827#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001828 return const_iterator(__p1_.first().__next_);
Howard Hinnantb24c8022013-07-23 22:01:58 +00001829#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001830}
1831
1832template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001833inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001834typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
Howard Hinnant37141072011-06-04 18:54:24 +00001835__hash_table<_Tp, _Hash, _Equal, _Alloc>::end() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001836{
Howard Hinnantb24c8022013-07-23 22:01:58 +00001837#if _LIBCPP_DEBUG_LEVEL >= 2
1838 return const_iterator(nullptr, this);
1839#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001840 return const_iterator(nullptr);
Howard Hinnantb24c8022013-07-23 22:01:58 +00001841#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001842}
1843
1844template <class _Tp, class _Hash, class _Equal, class _Alloc>
1845void
Howard Hinnant37141072011-06-04 18:54:24 +00001846__hash_table<_Tp, _Hash, _Equal, _Alloc>::clear() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001847{
1848 if (size() > 0)
1849 {
Eric Fiseliercd71f442017-01-07 03:01:24 +00001850 __deallocate_node(__p1_.first().__next_);
Howard Hinnant3e519522010-05-11 19:42:16 +00001851 __p1_.first().__next_ = nullptr;
1852 size_type __bc = bucket_count();
Howard Hinnant1f8da842011-07-05 14:14:17 +00001853 for (size_type __i = 0; __i < __bc; ++__i)
Howard Hinnant3e519522010-05-11 19:42:16 +00001854 __bucket_list_[__i] = nullptr;
1855 size() = 0;
1856 }
1857}
1858
Erik Pilkington5c4e07a2018-10-31 17:31:35 +00001859
1860// Prepare the container for an insertion of the value __value with the hash
1861// __hash. This does a lookup into the container to see if __value is already
1862// present, and performs a rehash if necessary. Returns a pointer to the
1863// existing element if it exists, otherwise nullptr.
1864//
1865// Note that this function does forward exceptions if key_eq() throws, and never
1866// mutates __value or actually inserts into the map.
Howard Hinnant3e519522010-05-11 19:42:16 +00001867template <class _Tp, class _Hash, class _Equal, class _Alloc>
Erik Pilkington5c4e07a2018-10-31 17:31:35 +00001868_LIBCPP_INLINE_VISIBILITY
1869typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__next_pointer
1870__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_unique_prepare(
1871 size_t __hash, value_type& __value)
Howard Hinnant3e519522010-05-11 19:42:16 +00001872{
Howard Hinnant3e519522010-05-11 19:42:16 +00001873 size_type __bc = bucket_count();
Erik Pilkington5c4e07a2018-10-31 17:31:35 +00001874
Howard Hinnant3e519522010-05-11 19:42:16 +00001875 if (__bc != 0)
1876 {
Erik Pilkington5c4e07a2018-10-31 17:31:35 +00001877 size_t __chash = __constrain_hash(__hash, __bc);
1878 __next_pointer __ndptr = __bucket_list_[__chash];
Howard Hinnant3e519522010-05-11 19:42:16 +00001879 if (__ndptr != nullptr)
1880 {
1881 for (__ndptr = __ndptr->__next_; __ndptr != nullptr &&
Eric Fiselier40492ba2016-07-23 20:36:55 +00001882 __constrain_hash(__ndptr->__hash(), __bc) == __chash;
Howard Hinnant3e519522010-05-11 19:42:16 +00001883 __ndptr = __ndptr->__next_)
1884 {
Erik Pilkington5c4e07a2018-10-31 17:31:35 +00001885 if (key_eq()(__ndptr->__upcast()->__value_, __value))
1886 return __ndptr;
Howard Hinnant3e519522010-05-11 19:42:16 +00001887 }
1888 }
1889 }
Erik Pilkington5c4e07a2018-10-31 17:31:35 +00001890 if (size()+1 > __bc * max_load_factor() || __bc == 0)
Howard Hinnant3e519522010-05-11 19:42:16 +00001891 {
Erik Pilkington5c4e07a2018-10-31 17:31:35 +00001892 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
1893 size_type(ceil(float(size() + 1) / max_load_factor()))));
Howard Hinnant3e519522010-05-11 19:42:16 +00001894 }
Erik Pilkington5c4e07a2018-10-31 17:31:35 +00001895 return nullptr;
1896}
1897
1898// Insert the node __nd into the container by pushing it into the right bucket,
1899// and updating size(). Assumes that __nd->__hash is up-to-date, and that
1900// rehashing has already occurred and that no element with the same key exists
1901// in the map.
1902template <class _Tp, class _Hash, class _Equal, class _Alloc>
1903_LIBCPP_INLINE_VISIBILITY
1904void
1905__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_unique_perform(
1906 __node_pointer __nd) _NOEXCEPT
1907{
1908 size_type __bc = bucket_count();
1909 size_t __chash = __constrain_hash(__nd->__hash(), __bc);
1910 // insert_after __bucket_list_[__chash], or __first_node if bucket is null
1911 __next_pointer __pn = __bucket_list_[__chash];
1912 if (__pn == nullptr)
1913 {
1914 __pn =__p1_.first().__ptr();
1915 __nd->__next_ = __pn->__next_;
1916 __pn->__next_ = __nd->__ptr();
1917 // fix up __bucket_list_
1918 __bucket_list_[__chash] = __pn;
1919 if (__nd->__next_ != nullptr)
1920 __bucket_list_[__constrain_hash(__nd->__next_->__hash(), __bc)] = __nd->__ptr();
1921 }
1922 else
1923 {
1924 __nd->__next_ = __pn->__next_;
1925 __pn->__next_ = __nd->__ptr();
1926 }
1927 ++size();
Howard Hinnant3e519522010-05-11 19:42:16 +00001928}
1929
1930template <class _Tp, class _Hash, class _Equal, class _Alloc>
Erik Pilkington5c4e07a2018-10-31 17:31:35 +00001931pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
1932__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_unique(__node_pointer __nd)
Howard Hinnant3e519522010-05-11 19:42:16 +00001933{
Erik Pilkington5c4e07a2018-10-31 17:31:35 +00001934 __nd->__hash_ = hash_function()(__nd->__value_);
1935 __next_pointer __existing_node =
1936 __node_insert_unique_prepare(__nd->__hash(), __nd->__value_);
1937
1938 // Insert the node, unless it already exists in the container.
1939 bool __inserted = false;
1940 if (__existing_node == nullptr)
1941 {
1942 __node_insert_unique_perform(__nd);
1943 __existing_node = __nd->__ptr();
1944 __inserted = true;
1945 }
1946#if _LIBCPP_DEBUG_LEVEL >= 2
1947 return pair<iterator, bool>(iterator(__existing_node, this), __inserted);
1948#else
1949 return pair<iterator, bool>(iterator(__existing_node), __inserted);
1950#endif
1951}
1952
1953// Prepare the container for an insertion of the value __cp_val with the hash
1954// __cp_hash. This does a lookup into the container to see if __cp_value is
1955// already present, and performs a rehash if necessary. Returns a pointer to the
1956// last occurance of __cp_val in the map.
1957//
1958// Note that this function does forward exceptions if key_eq() throws, and never
1959// mutates __value or actually inserts into the map.
1960template <class _Tp, class _Hash, class _Equal, class _Alloc>
1961typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__next_pointer
1962__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi_prepare(
1963 size_t __cp_hash, value_type& __cp_val)
1964{
Howard Hinnant3e519522010-05-11 19:42:16 +00001965 size_type __bc = bucket_count();
1966 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1967 {
Eric Fiselier9ba5c112015-02-02 21:31:48 +00001968 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnant3e519522010-05-11 19:42:16 +00001969 size_type(ceil(float(size() + 1) / max_load_factor()))));
1970 __bc = bucket_count();
1971 }
Erik Pilkington5c4e07a2018-10-31 17:31:35 +00001972 size_t __chash = __constrain_hash(__cp_hash, __bc);
Eric Fiselier40492ba2016-07-23 20:36:55 +00001973 __next_pointer __pn = __bucket_list_[__chash];
Erik Pilkington5c4e07a2018-10-31 17:31:35 +00001974 if (__pn != nullptr)
1975 {
1976 for (bool __found = false; __pn->__next_ != nullptr &&
1977 __constrain_hash(__pn->__next_->__hash(), __bc) == __chash;
1978 __pn = __pn->__next_)
1979 {
1980 // __found key_eq() action
1981 // false false loop
1982 // true true loop
1983 // false true set __found to true
1984 // true false break
1985 if (__found != (__pn->__next_->__hash() == __cp_hash &&
1986 key_eq()(__pn->__next_->__upcast()->__value_, __cp_val)))
1987 {
1988 if (!__found)
1989 __found = true;
1990 else
1991 break;
1992 }
1993 }
1994 }
1995 return __pn;
1996}
1997
1998// Insert the node __cp into the container after __pn (which is the last node in
1999// the bucket that compares equal to __cp). Rehashing, and checking for
2000// uniqueness has already been performed (in __node_insert_multi_prepare), so
2001// all we need to do is update the bucket and size(). Assumes that __cp->__hash
2002// is up-to-date.
2003template <class _Tp, class _Hash, class _Equal, class _Alloc>
2004void
2005__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi_perform(
2006 __node_pointer __cp, __next_pointer __pn) _NOEXCEPT
2007{
2008 size_type __bc = bucket_count();
2009 size_t __chash = __constrain_hash(__cp->__hash_, __bc);
Howard Hinnant3e519522010-05-11 19:42:16 +00002010 if (__pn == nullptr)
2011 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00002012 __pn =__p1_.first().__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00002013 __cp->__next_ = __pn->__next_;
Eric Fiselier40492ba2016-07-23 20:36:55 +00002014 __pn->__next_ = __cp->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00002015 // fix up __bucket_list_
2016 __bucket_list_[__chash] = __pn;
2017 if (__cp->__next_ != nullptr)
Eric Fiselier40492ba2016-07-23 20:36:55 +00002018 __bucket_list_[__constrain_hash(__cp->__next_->__hash(), __bc)]
2019 = __cp->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00002020 }
2021 else
2022 {
Howard Hinnant3e519522010-05-11 19:42:16 +00002023 __cp->__next_ = __pn->__next_;
Eric Fiselier40492ba2016-07-23 20:36:55 +00002024 __pn->__next_ = __cp->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00002025 if (__cp->__next_ != nullptr)
2026 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00002027 size_t __nhash = __constrain_hash(__cp->__next_->__hash(), __bc);
Howard Hinnant3e519522010-05-11 19:42:16 +00002028 if (__nhash != __chash)
Eric Fiselier40492ba2016-07-23 20:36:55 +00002029 __bucket_list_[__nhash] = __cp->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00002030 }
2031 }
2032 ++size();
Erik Pilkington5c4e07a2018-10-31 17:31:35 +00002033}
2034
2035
2036template <class _Tp, class _Hash, class _Equal, class _Alloc>
2037typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2038__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi(__node_pointer __cp)
2039{
2040 __cp->__hash_ = hash_function()(__cp->__value_);
2041 __next_pointer __pn = __node_insert_multi_prepare(__cp->__hash(), __cp->__value_);
2042 __node_insert_multi_perform(__cp, __pn);
2043
Howard Hinnantb24c8022013-07-23 22:01:58 +00002044#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselier40492ba2016-07-23 20:36:55 +00002045 return iterator(__cp->__ptr(), this);
Howard Hinnantb24c8022013-07-23 22:01:58 +00002046#else
Eric Fiselier40492ba2016-07-23 20:36:55 +00002047 return iterator(__cp->__ptr());
Howard Hinnantb24c8022013-07-23 22:01:58 +00002048#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002049}
2050
2051template <class _Tp, class _Hash, class _Equal, class _Alloc>
2052typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2053__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi(
2054 const_iterator __p, __node_pointer __cp)
2055{
Howard Hinnant2f51de52013-08-02 17:50:49 +00002056#if _LIBCPP_DEBUG_LEVEL >= 2
2057 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2058 "unordered container::emplace_hint(const_iterator, args...) called with an iterator not"
2059 " referring to this unordered container");
2060#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002061 if (__p != end() && key_eq()(*__p, __cp->__value_))
2062 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00002063 __next_pointer __np = __p.__node_;
2064 __cp->__hash_ = __np->__hash();
Howard Hinnant3e519522010-05-11 19:42:16 +00002065 size_type __bc = bucket_count();
2066 if (size()+1 > __bc * max_load_factor() || __bc == 0)
2067 {
Eric Fiselier9ba5c112015-02-02 21:31:48 +00002068 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnant3e519522010-05-11 19:42:16 +00002069 size_type(ceil(float(size() + 1) / max_load_factor()))));
2070 __bc = bucket_count();
2071 }
Howard Hinnant4cb38a82012-07-06 17:31:14 +00002072 size_t __chash = __constrain_hash(__cp->__hash_, __bc);
Eric Fiselier40492ba2016-07-23 20:36:55 +00002073 __next_pointer __pp = __bucket_list_[__chash];
Howard Hinnant3e519522010-05-11 19:42:16 +00002074 while (__pp->__next_ != __np)
2075 __pp = __pp->__next_;
2076 __cp->__next_ = __np;
Eric Fiselier40492ba2016-07-23 20:36:55 +00002077 __pp->__next_ = static_cast<__next_pointer>(__cp);
Howard Hinnant3e519522010-05-11 19:42:16 +00002078 ++size();
Howard Hinnantb24c8022013-07-23 22:01:58 +00002079#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselier40492ba2016-07-23 20:36:55 +00002080 return iterator(static_cast<__next_pointer>(__cp), this);
Howard Hinnantb24c8022013-07-23 22:01:58 +00002081#else
Eric Fiselier40492ba2016-07-23 20:36:55 +00002082 return iterator(static_cast<__next_pointer>(__cp));
Howard Hinnantb24c8022013-07-23 22:01:58 +00002083#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002084 }
2085 return __node_insert_multi(__cp);
2086}
2087
Eric Fiselierde3f2b32015-06-13 07:18:32 +00002088
2089
Eric Fiselierfcd02212016-02-11 11:59:44 +00002090#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierde3f2b32015-06-13 07:18:32 +00002091template <class _Tp, class _Hash, class _Equal, class _Alloc>
Eric Fiselierfcd02212016-02-11 11:59:44 +00002092template <class _Key, class ..._Args>
Eric Fiselierde3f2b32015-06-13 07:18:32 +00002093pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
Eric Fiselierfcd02212016-02-11 11:59:44 +00002094__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_key_args(_Key const& __k, _Args&&... __args)
Eric Fiselierde3f2b32015-06-13 07:18:32 +00002095#else
2096template <class _Tp, class _Hash, class _Equal, class _Alloc>
Eric Fiselierfcd02212016-02-11 11:59:44 +00002097template <class _Key, class _Args>
Eric Fiselierde3f2b32015-06-13 07:18:32 +00002098pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
Eric Fiselierfcd02212016-02-11 11:59:44 +00002099__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_key_args(_Key const& __k, _Args& __args)
Eric Fiselierde3f2b32015-06-13 07:18:32 +00002100#endif
2101{
Eric Fiselierfcd02212016-02-11 11:59:44 +00002102
2103 size_t __hash = hash_function()(__k);
Howard Hinnant3e519522010-05-11 19:42:16 +00002104 size_type __bc = bucket_count();
2105 bool __inserted = false;
Eric Fiselier40492ba2016-07-23 20:36:55 +00002106 __next_pointer __nd;
Howard Hinnant3e519522010-05-11 19:42:16 +00002107 size_t __chash;
2108 if (__bc != 0)
2109 {
Howard Hinnant4cb38a82012-07-06 17:31:14 +00002110 __chash = __constrain_hash(__hash, __bc);
Howard Hinnant3e519522010-05-11 19:42:16 +00002111 __nd = __bucket_list_[__chash];
2112 if (__nd != nullptr)
2113 {
2114 for (__nd = __nd->__next_; __nd != nullptr &&
Eric Fiselier1a06fe52016-07-24 06:22:25 +00002115 (__nd->__hash() == __hash || __constrain_hash(__nd->__hash(), __bc) == __chash);
Howard Hinnant3e519522010-05-11 19:42:16 +00002116 __nd = __nd->__next_)
2117 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00002118 if (key_eq()(__nd->__upcast()->__value_, __k))
Howard Hinnant3e519522010-05-11 19:42:16 +00002119 goto __done;
2120 }
2121 }
2122 }
2123 {
Eric Fiselierfcd02212016-02-11 11:59:44 +00002124#ifndef _LIBCPP_CXX03_LANG
2125 __node_holder __h = __construct_node_hash(__hash, _VSTD::forward<_Args>(__args)...);
2126#else
2127 __node_holder __h = __construct_node_hash(__hash, __args);
2128#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002129 if (size()+1 > __bc * max_load_factor() || __bc == 0)
2130 {
Eric Fiselier9ba5c112015-02-02 21:31:48 +00002131 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnant3e519522010-05-11 19:42:16 +00002132 size_type(ceil(float(size() + 1) / max_load_factor()))));
2133 __bc = bucket_count();
Howard Hinnant4cb38a82012-07-06 17:31:14 +00002134 __chash = __constrain_hash(__hash, __bc);
Howard Hinnant3e519522010-05-11 19:42:16 +00002135 }
2136 // insert_after __bucket_list_[__chash], or __first_node if bucket is null
Eric Fiselier40492ba2016-07-23 20:36:55 +00002137 __next_pointer __pn = __bucket_list_[__chash];
Howard Hinnant3e519522010-05-11 19:42:16 +00002138 if (__pn == nullptr)
2139 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00002140 __pn = __p1_.first().__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00002141 __h->__next_ = __pn->__next_;
Eric Fiselier40492ba2016-07-23 20:36:55 +00002142 __pn->__next_ = __h.get()->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00002143 // fix up __bucket_list_
2144 __bucket_list_[__chash] = __pn;
2145 if (__h->__next_ != nullptr)
Eric Fiselier40492ba2016-07-23 20:36:55 +00002146 __bucket_list_[__constrain_hash(__h->__next_->__hash(), __bc)]
2147 = __h.get()->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00002148 }
2149 else
2150 {
2151 __h->__next_ = __pn->__next_;
Eric Fiselier40492ba2016-07-23 20:36:55 +00002152 __pn->__next_ = static_cast<__next_pointer>(__h.get());
Howard Hinnant3e519522010-05-11 19:42:16 +00002153 }
Eric Fiselier40492ba2016-07-23 20:36:55 +00002154 __nd = static_cast<__next_pointer>(__h.release());
Howard Hinnant3e519522010-05-11 19:42:16 +00002155 // increment size
2156 ++size();
2157 __inserted = true;
2158 }
2159__done:
Howard Hinnantb24c8022013-07-23 22:01:58 +00002160#if _LIBCPP_DEBUG_LEVEL >= 2
2161 return pair<iterator, bool>(iterator(__nd, this), __inserted);
2162#else
Howard Hinnant3e519522010-05-11 19:42:16 +00002163 return pair<iterator, bool>(iterator(__nd), __inserted);
Howard Hinnantb24c8022013-07-23 22:01:58 +00002164#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002165}
2166
Eric Fiselierfcd02212016-02-11 11:59:44 +00002167#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00002168
2169template <class _Tp, class _Hash, class _Equal, class _Alloc>
2170template <class... _Args>
2171pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
Duncan P. N. Exon Smithfde79b42016-03-17 20:45:20 +00002172__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_impl(_Args&&... __args)
Howard Hinnant3e519522010-05-11 19:42:16 +00002173{
Howard Hinnantce48a112011-06-30 21:18:19 +00002174 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnant3e519522010-05-11 19:42:16 +00002175 pair<iterator, bool> __r = __node_insert_unique(__h.get());
2176 if (__r.second)
2177 __h.release();
2178 return __r;
2179}
2180
2181template <class _Tp, class _Hash, class _Equal, class _Alloc>
2182template <class... _Args>
2183typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2184__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_multi(_Args&&... __args)
2185{
Howard Hinnantce48a112011-06-30 21:18:19 +00002186 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnant3e519522010-05-11 19:42:16 +00002187 iterator __r = __node_insert_multi(__h.get());
2188 __h.release();
2189 return __r;
2190}
2191
2192template <class _Tp, class _Hash, class _Equal, class _Alloc>
2193template <class... _Args>
2194typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2195__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_hint_multi(
2196 const_iterator __p, _Args&&... __args)
2197{
Howard Hinnante5c13de2013-07-29 19:05:47 +00002198#if _LIBCPP_DEBUG_LEVEL >= 2
2199 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2200 "unordered container::emplace_hint(const_iterator, args...) called with an iterator not"
2201 " referring to this unordered container");
2202#endif
Howard Hinnantce48a112011-06-30 21:18:19 +00002203 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnant3e519522010-05-11 19:42:16 +00002204 iterator __r = __node_insert_multi(__p, __h.get());
2205 __h.release();
2206 return __r;
2207}
2208
Eric Fiselierfcd02212016-02-11 11:59:44 +00002209#else // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00002210
2211template <class _Tp, class _Hash, class _Equal, class _Alloc>
2212typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
Eric Fiselierfcd02212016-02-11 11:59:44 +00002213__hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const __container_value_type& __x)
Howard Hinnant3e519522010-05-11 19:42:16 +00002214{
2215 __node_holder __h = __construct_node(__x);
2216 iterator __r = __node_insert_multi(__h.get());
2217 __h.release();
2218 return __r;
2219}
2220
2221template <class _Tp, class _Hash, class _Equal, class _Alloc>
2222typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2223__hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const_iterator __p,
Eric Fiselierfcd02212016-02-11 11:59:44 +00002224 const __container_value_type& __x)
Howard Hinnant3e519522010-05-11 19:42:16 +00002225{
Howard Hinnante5c13de2013-07-29 19:05:47 +00002226#if _LIBCPP_DEBUG_LEVEL >= 2
2227 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2228 "unordered container::insert(const_iterator, lvalue) called with an iterator not"
2229 " referring to this unordered container");
2230#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002231 __node_holder __h = __construct_node(__x);
2232 iterator __r = __node_insert_multi(__p, __h.get());
2233 __h.release();
2234 return __r;
2235}
2236
Eric Fiselierfcd02212016-02-11 11:59:44 +00002237#endif // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00002238
Erik Pilkingtonb0386a52018-08-01 01:33:38 +00002239#if _LIBCPP_STD_VER > 14
2240template <class _Tp, class _Hash, class _Equal, class _Alloc>
2241template <class _NodeHandle, class _InsertReturnType>
2242_LIBCPP_INLINE_VISIBILITY
2243_InsertReturnType
2244__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_insert_unique(
2245 _NodeHandle&& __nh)
2246{
2247 if (__nh.empty())
2248 return _InsertReturnType{end(), false, _NodeHandle()};
2249 pair<iterator, bool> __result = __node_insert_unique(__nh.__ptr_);
2250 if (__result.second)
2251 __nh.__release();
2252 return _InsertReturnType{__result.first, __result.second, _VSTD::move(__nh)};
2253}
2254
2255template <class _Tp, class _Hash, class _Equal, class _Alloc>
2256template <class _NodeHandle>
2257_LIBCPP_INLINE_VISIBILITY
2258typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2259__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_insert_unique(
2260 const_iterator, _NodeHandle&& __nh)
2261{
2262 if (__nh.empty())
2263 return end();
2264 pair<iterator, bool> __result = __node_insert_unique(__nh.__ptr_);
2265 if (__result.second)
2266 __nh.__release();
2267 return __result.first;
2268}
2269
2270template <class _Tp, class _Hash, class _Equal, class _Alloc>
2271template <class _NodeHandle>
2272_LIBCPP_INLINE_VISIBILITY
2273_NodeHandle
2274__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_extract(
2275 key_type const& __key)
2276{
2277 iterator __i = find(__key);
2278 if (__i == end())
2279 return _NodeHandle();
2280 return __node_handle_extract<_NodeHandle>(__i);
2281}
2282
2283template <class _Tp, class _Hash, class _Equal, class _Alloc>
2284template <class _NodeHandle>
2285_LIBCPP_INLINE_VISIBILITY
2286_NodeHandle
2287__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_extract(
2288 const_iterator __p)
2289{
2290 allocator_type __alloc(__node_alloc());
2291 return _NodeHandle(remove(__p).release(), __alloc);
2292}
2293
2294template <class _Tp, class _Hash, class _Equal, class _Alloc>
Erik Pilkington5c4e07a2018-10-31 17:31:35 +00002295template <class _Table>
2296_LIBCPP_INLINE_VISIBILITY
2297void
2298__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_merge_unique(
2299 _Table& __source)
2300{
2301 static_assert(is_same<__node, typename _Table::__node>::value, "");
2302
2303 for (typename _Table::iterator __it = __source.begin();
2304 __it != __source.end();)
2305 {
2306 __node_pointer __src_ptr = __it.__node_->__upcast();
2307 size_t __hash = hash_function()(__src_ptr->__value_);
2308 __next_pointer __existing_node =
2309 __node_insert_unique_prepare(__hash, __src_ptr->__value_);
2310 auto __prev_iter = __it++;
2311 if (__existing_node == nullptr)
2312 {
2313 (void)__source.remove(__prev_iter).release();
2314 __src_ptr->__hash_ = __hash;
2315 __node_insert_unique_perform(__src_ptr);
2316 }
2317 }
2318}
2319
2320template <class _Tp, class _Hash, class _Equal, class _Alloc>
Erik Pilkingtonb0386a52018-08-01 01:33:38 +00002321template <class _NodeHandle>
2322_LIBCPP_INLINE_VISIBILITY
2323typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2324__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_insert_multi(
2325 _NodeHandle&& __nh)
2326{
2327 if (__nh.empty())
2328 return end();
2329 iterator __result = __node_insert_multi(__nh.__ptr_);
2330 __nh.__release();
2331 return __result;
2332}
2333
2334template <class _Tp, class _Hash, class _Equal, class _Alloc>
2335template <class _NodeHandle>
2336_LIBCPP_INLINE_VISIBILITY
2337typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2338__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_insert_multi(
2339 const_iterator __hint, _NodeHandle&& __nh)
2340{
2341 if (__nh.empty())
2342 return end();
2343 iterator __result = __node_insert_multi(__hint, __nh.__ptr_);
2344 __nh.__release();
2345 return __result;
2346}
2347
Erik Pilkington5c4e07a2018-10-31 17:31:35 +00002348template <class _Tp, class _Hash, class _Equal, class _Alloc>
2349template <class _Table>
2350_LIBCPP_INLINE_VISIBILITY
2351void
2352__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_merge_multi(
2353 _Table& __source)
2354{
2355 static_assert(is_same<typename _Table::__node, __node>::value, "");
2356
2357 for (typename _Table::iterator __it = __source.begin();
2358 __it != __source.end();)
2359 {
2360 __node_pointer __src_ptr = __it.__node_->__upcast();
2361 size_t __src_hash = hash_function()(__src_ptr->__value_);
2362 __next_pointer __pn =
2363 __node_insert_multi_prepare(__src_hash, __src_ptr->__value_);
2364 (void)__source.remove(__it++).release();
2365 __src_ptr->__hash_ = __src_hash;
2366 __node_insert_multi_perform(__src_ptr, __pn);
2367 }
2368}
Erik Pilkingtonb0386a52018-08-01 01:33:38 +00002369#endif // _LIBCPP_STD_VER > 14
2370
Howard Hinnant3e519522010-05-11 19:42:16 +00002371template <class _Tp, class _Hash, class _Equal, class _Alloc>
2372void
2373__hash_table<_Tp, _Hash, _Equal, _Alloc>::rehash(size_type __n)
2374{
Dan Albert553b09b2018-01-08 22:57:12 +00002375 if (__n == 1)
Howard Hinnant4cb38a82012-07-06 17:31:14 +00002376 __n = 2;
2377 else if (__n & (__n - 1))
2378 __n = __next_prime(__n);
Howard Hinnant3e519522010-05-11 19:42:16 +00002379 size_type __bc = bucket_count();
2380 if (__n > __bc)
2381 __rehash(__n);
Howard Hinnant4cb38a82012-07-06 17:31:14 +00002382 else if (__n < __bc)
Howard Hinnant3e519522010-05-11 19:42:16 +00002383 {
Howard Hinnantce48a112011-06-30 21:18:19 +00002384 __n = _VSTD::max<size_type>
Howard Hinnant3e519522010-05-11 19:42:16 +00002385 (
2386 __n,
Eric Fiselier9ba5c112015-02-02 21:31:48 +00002387 __is_hash_power2(__bc) ? __next_hash_pow2(size_t(ceil(float(size()) / max_load_factor()))) :
2388 __next_prime(size_t(ceil(float(size()) / max_load_factor())))
Howard Hinnant3e519522010-05-11 19:42:16 +00002389 );
2390 if (__n < __bc)
2391 __rehash(__n);
2392 }
2393}
2394
2395template <class _Tp, class _Hash, class _Equal, class _Alloc>
2396void
2397__hash_table<_Tp, _Hash, _Equal, _Alloc>::__rehash(size_type __nbc)
2398{
Howard Hinnantb24c8022013-07-23 22:01:58 +00002399#if _LIBCPP_DEBUG_LEVEL >= 2
2400 __get_db()->__invalidate_all(this);
2401#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant3e519522010-05-11 19:42:16 +00002402 __pointer_allocator& __npa = __bucket_list_.get_deleter().__alloc();
2403 __bucket_list_.reset(__nbc > 0 ?
2404 __pointer_alloc_traits::allocate(__npa, __nbc) : nullptr);
2405 __bucket_list_.get_deleter().size() = __nbc;
2406 if (__nbc > 0)
2407 {
2408 for (size_type __i = 0; __i < __nbc; ++__i)
2409 __bucket_list_[__i] = nullptr;
Eric Fiselier40492ba2016-07-23 20:36:55 +00002410 __next_pointer __pp = __p1_.first().__ptr();
2411 __next_pointer __cp = __pp->__next_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002412 if (__cp != nullptr)
2413 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00002414 size_type __chash = __constrain_hash(__cp->__hash(), __nbc);
Howard Hinnant3e519522010-05-11 19:42:16 +00002415 __bucket_list_[__chash] = __pp;
2416 size_type __phash = __chash;
2417 for (__pp = __cp, __cp = __cp->__next_; __cp != nullptr;
2418 __cp = __pp->__next_)
2419 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00002420 __chash = __constrain_hash(__cp->__hash(), __nbc);
Howard Hinnant3e519522010-05-11 19:42:16 +00002421 if (__chash == __phash)
2422 __pp = __cp;
2423 else
2424 {
2425 if (__bucket_list_[__chash] == nullptr)
2426 {
2427 __bucket_list_[__chash] = __pp;
2428 __pp = __cp;
2429 __phash = __chash;
2430 }
2431 else
2432 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00002433 __next_pointer __np = __cp;
Howard Hinnant3e519522010-05-11 19:42:16 +00002434 for (; __np->__next_ != nullptr &&
Eric Fiselier40492ba2016-07-23 20:36:55 +00002435 key_eq()(__cp->__upcast()->__value_,
2436 __np->__next_->__upcast()->__value_);
Howard Hinnant3e519522010-05-11 19:42:16 +00002437 __np = __np->__next_)
2438 ;
2439 __pp->__next_ = __np->__next_;
2440 __np->__next_ = __bucket_list_[__chash]->__next_;
2441 __bucket_list_[__chash]->__next_ = __cp;
Howard Hinnantb3371f62010-08-22 00:02:43 +00002442
Howard Hinnant3e519522010-05-11 19:42:16 +00002443 }
2444 }
2445 }
2446 }
2447 }
2448}
2449
2450template <class _Tp, class _Hash, class _Equal, class _Alloc>
2451template <class _Key>
2452typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2453__hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k)
2454{
2455 size_t __hash = hash_function()(__k);
2456 size_type __bc = bucket_count();
2457 if (__bc != 0)
2458 {
Howard Hinnant4cb38a82012-07-06 17:31:14 +00002459 size_t __chash = __constrain_hash(__hash, __bc);
Eric Fiselier40492ba2016-07-23 20:36:55 +00002460 __next_pointer __nd = __bucket_list_[__chash];
Howard Hinnant3e519522010-05-11 19:42:16 +00002461 if (__nd != nullptr)
2462 {
2463 for (__nd = __nd->__next_; __nd != nullptr &&
Eric Fiselier40492ba2016-07-23 20:36:55 +00002464 (__nd->__hash() == __hash
2465 || __constrain_hash(__nd->__hash(), __bc) == __chash);
Howard Hinnant3e519522010-05-11 19:42:16 +00002466 __nd = __nd->__next_)
2467 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00002468 if ((__nd->__hash() == __hash)
2469 && key_eq()(__nd->__upcast()->__value_, __k))
Howard Hinnantb24c8022013-07-23 22:01:58 +00002470#if _LIBCPP_DEBUG_LEVEL >= 2
2471 return iterator(__nd, this);
2472#else
Howard Hinnant3e519522010-05-11 19:42:16 +00002473 return iterator(__nd);
Howard Hinnantb24c8022013-07-23 22:01:58 +00002474#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002475 }
2476 }
2477 }
2478 return end();
2479}
2480
2481template <class _Tp, class _Hash, class _Equal, class _Alloc>
2482template <class _Key>
2483typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
2484__hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k) const
2485{
2486 size_t __hash = hash_function()(__k);
2487 size_type __bc = bucket_count();
2488 if (__bc != 0)
2489 {
Howard Hinnant4cb38a82012-07-06 17:31:14 +00002490 size_t __chash = __constrain_hash(__hash, __bc);
Eric Fiselier40492ba2016-07-23 20:36:55 +00002491 __next_pointer __nd = __bucket_list_[__chash];
Howard Hinnant3e519522010-05-11 19:42:16 +00002492 if (__nd != nullptr)
2493 {
2494 for (__nd = __nd->__next_; __nd != nullptr &&
Eric Fiselier40492ba2016-07-23 20:36:55 +00002495 (__hash == __nd->__hash()
2496 || __constrain_hash(__nd->__hash(), __bc) == __chash);
Howard Hinnant3e519522010-05-11 19:42:16 +00002497 __nd = __nd->__next_)
2498 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00002499 if ((__nd->__hash() == __hash)
2500 && key_eq()(__nd->__upcast()->__value_, __k))
Howard Hinnantb24c8022013-07-23 22:01:58 +00002501#if _LIBCPP_DEBUG_LEVEL >= 2
2502 return const_iterator(__nd, this);
2503#else
Howard Hinnant3e519522010-05-11 19:42:16 +00002504 return const_iterator(__nd);
Howard Hinnantb24c8022013-07-23 22:01:58 +00002505#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002506 }
2507 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00002508
Howard Hinnant3e519522010-05-11 19:42:16 +00002509 }
2510 return end();
2511}
2512
Eric Fiselierfcd02212016-02-11 11:59:44 +00002513#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00002514
2515template <class _Tp, class _Hash, class _Equal, class _Alloc>
2516template <class ..._Args>
2517typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
2518__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(_Args&& ...__args)
2519{
Eric Fiselierfcd02212016-02-11 11:59:44 +00002520 static_assert(!__is_hash_value_type<_Args...>::value,
2521 "Construct cannot be called with a hash value type");
Howard Hinnant3e519522010-05-11 19:42:16 +00002522 __node_allocator& __na = __node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00002523 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselierfcd02212016-02-11 11:59:44 +00002524 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), _VSTD::forward<_Args>(__args)...);
Howard Hinnant3e519522010-05-11 19:42:16 +00002525 __h.get_deleter().__value_constructed = true;
2526 __h->__hash_ = hash_function()(__h->__value_);
2527 __h->__next_ = nullptr;
2528 return __h;
2529}
2530
2531template <class _Tp, class _Hash, class _Equal, class _Alloc>
Eric Fiselierfcd02212016-02-11 11:59:44 +00002532template <class _First, class ..._Rest>
Howard Hinnant3e519522010-05-11 19:42:16 +00002533typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Eric Fiselierfcd02212016-02-11 11:59:44 +00002534__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node_hash(
2535 size_t __hash, _First&& __f, _Rest&& ...__rest)
Howard Hinnant3e519522010-05-11 19:42:16 +00002536{
Eric Fiselierfcd02212016-02-11 11:59:44 +00002537 static_assert(!__is_hash_value_type<_First, _Rest...>::value,
2538 "Construct cannot be called with a hash value type");
Howard Hinnant3e519522010-05-11 19:42:16 +00002539 __node_allocator& __na = __node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00002540 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselierfcd02212016-02-11 11:59:44 +00002541 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_),
2542 _VSTD::forward<_First>(__f),
2543 _VSTD::forward<_Rest>(__rest)...);
Howard Hinnant3e519522010-05-11 19:42:16 +00002544 __h.get_deleter().__value_constructed = true;
2545 __h->__hash_ = __hash;
2546 __h->__next_ = nullptr;
Howard Hinnant179b1f82013-08-22 18:29:50 +00002547 return __h;
Howard Hinnant3e519522010-05-11 19:42:16 +00002548}
2549
Eric Fiselierfcd02212016-02-11 11:59:44 +00002550#else // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00002551
2552template <class _Tp, class _Hash, class _Equal, class _Alloc>
2553typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Eric Fiselierfcd02212016-02-11 11:59:44 +00002554__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(const __container_value_type& __v)
Howard Hinnant3e519522010-05-11 19:42:16 +00002555{
2556 __node_allocator& __na = __node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00002557 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselierfcd02212016-02-11 11:59:44 +00002558 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), __v);
Howard Hinnant3e519522010-05-11 19:42:16 +00002559 __h.get_deleter().__value_constructed = true;
2560 __h->__hash_ = hash_function()(__h->__value_);
2561 __h->__next_ = nullptr;
Dimitry Andric251c6292015-08-19 06:43:33 +00002562 return _LIBCPP_EXPLICIT_MOVE(__h); // explicitly moved for C++03
Howard Hinnant3e519522010-05-11 19:42:16 +00002563}
2564
Howard Hinnant3e519522010-05-11 19:42:16 +00002565template <class _Tp, class _Hash, class _Equal, class _Alloc>
2566typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Eric Fiselierfcd02212016-02-11 11:59:44 +00002567__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node_hash(size_t __hash,
2568 const __container_value_type& __v)
Howard Hinnant3e519522010-05-11 19:42:16 +00002569{
2570 __node_allocator& __na = __node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00002571 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselierfcd02212016-02-11 11:59:44 +00002572 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), __v);
Howard Hinnant3e519522010-05-11 19:42:16 +00002573 __h.get_deleter().__value_constructed = true;
2574 __h->__hash_ = __hash;
2575 __h->__next_ = nullptr;
Dimitry Andric251c6292015-08-19 06:43:33 +00002576 return _LIBCPP_EXPLICIT_MOVE(__h); // explicitly moved for C++03
Howard Hinnant3e519522010-05-11 19:42:16 +00002577}
2578
Eric Fiselierfcd02212016-02-11 11:59:44 +00002579#endif // _LIBCPP_CXX03_LANG
2580
Howard Hinnant3e519522010-05-11 19:42:16 +00002581template <class _Tp, class _Hash, class _Equal, class _Alloc>
2582typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2583__hash_table<_Tp, _Hash, _Equal, _Alloc>::erase(const_iterator __p)
2584{
Eric Fiselier40492ba2016-07-23 20:36:55 +00002585 __next_pointer __np = __p.__node_;
Howard Hinnantb24c8022013-07-23 22:01:58 +00002586#if _LIBCPP_DEBUG_LEVEL >= 2
2587 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2588 "unordered container erase(iterator) called with an iterator not"
2589 " referring to this container");
2590 _LIBCPP_ASSERT(__p != end(),
2591 "unordered container erase(iterator) called with a non-dereferenceable iterator");
2592 iterator __r(__np, this);
2593#else
Howard Hinnant3e519522010-05-11 19:42:16 +00002594 iterator __r(__np);
Howard Hinnantb24c8022013-07-23 22:01:58 +00002595#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002596 ++__r;
2597 remove(__p);
2598 return __r;
2599}
2600
2601template <class _Tp, class _Hash, class _Equal, class _Alloc>
2602typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2603__hash_table<_Tp, _Hash, _Equal, _Alloc>::erase(const_iterator __first,
2604 const_iterator __last)
2605{
Howard Hinnantb24c8022013-07-23 22:01:58 +00002606#if _LIBCPP_DEBUG_LEVEL >= 2
2607 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
2608 "unodered container::erase(iterator, iterator) called with an iterator not"
2609 " referring to this unodered container");
2610 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__last) == this,
2611 "unodered container::erase(iterator, iterator) called with an iterator not"
2612 " referring to this unodered container");
2613#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002614 for (const_iterator __p = __first; __first != __last; __p = __first)
2615 {
2616 ++__first;
2617 erase(__p);
2618 }
Eric Fiselier40492ba2016-07-23 20:36:55 +00002619 __next_pointer __np = __last.__node_;
Howard Hinnantb24c8022013-07-23 22:01:58 +00002620#if _LIBCPP_DEBUG_LEVEL >= 2
2621 return iterator (__np, this);
2622#else
Howard Hinnant3e519522010-05-11 19:42:16 +00002623 return iterator (__np);
Howard Hinnantb24c8022013-07-23 22:01:58 +00002624#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002625}
2626
2627template <class _Tp, class _Hash, class _Equal, class _Alloc>
2628template <class _Key>
2629typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2630__hash_table<_Tp, _Hash, _Equal, _Alloc>::__erase_unique(const _Key& __k)
2631{
2632 iterator __i = find(__k);
2633 if (__i == end())
2634 return 0;
2635 erase(__i);
2636 return 1;
2637}
2638
2639template <class _Tp, class _Hash, class _Equal, class _Alloc>
2640template <class _Key>
2641typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2642__hash_table<_Tp, _Hash, _Equal, _Alloc>::__erase_multi(const _Key& __k)
2643{
2644 size_type __r = 0;
2645 iterator __i = find(__k);
2646 if (__i != end())
2647 {
2648 iterator __e = end();
2649 do
2650 {
2651 erase(__i++);
2652 ++__r;
2653 } while (__i != __e && key_eq()(*__i, __k));
2654 }
2655 return __r;
2656}
2657
2658template <class _Tp, class _Hash, class _Equal, class _Alloc>
2659typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Howard Hinnant37141072011-06-04 18:54:24 +00002660__hash_table<_Tp, _Hash, _Equal, _Alloc>::remove(const_iterator __p) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00002661{
2662 // current node
Eric Fiselier40492ba2016-07-23 20:36:55 +00002663 __next_pointer __cn = __p.__node_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002664 size_type __bc = bucket_count();
Eric Fiselier40492ba2016-07-23 20:36:55 +00002665 size_t __chash = __constrain_hash(__cn->__hash(), __bc);
Howard Hinnant3e519522010-05-11 19:42:16 +00002666 // find previous node
Eric Fiselier40492ba2016-07-23 20:36:55 +00002667 __next_pointer __pn = __bucket_list_[__chash];
Howard Hinnant3e519522010-05-11 19:42:16 +00002668 for (; __pn->__next_ != __cn; __pn = __pn->__next_)
2669 ;
2670 // Fix up __bucket_list_
2671 // if __pn is not in same bucket (before begin is not in same bucket) &&
2672 // if __cn->__next_ is not in same bucket (nullptr is not in same bucket)
Eric Fiselier40492ba2016-07-23 20:36:55 +00002673 if (__pn == __p1_.first().__ptr()
2674 || __constrain_hash(__pn->__hash(), __bc) != __chash)
Howard Hinnant3e519522010-05-11 19:42:16 +00002675 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00002676 if (__cn->__next_ == nullptr
2677 || __constrain_hash(__cn->__next_->__hash(), __bc) != __chash)
Howard Hinnant3e519522010-05-11 19:42:16 +00002678 __bucket_list_[__chash] = nullptr;
2679 }
2680 // if __cn->__next_ is not in same bucket (nullptr is in same bucket)
2681 if (__cn->__next_ != nullptr)
2682 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00002683 size_t __nhash = __constrain_hash(__cn->__next_->__hash(), __bc);
Howard Hinnant3e519522010-05-11 19:42:16 +00002684 if (__nhash != __chash)
2685 __bucket_list_[__nhash] = __pn;
2686 }
2687 // remove __cn
2688 __pn->__next_ = __cn->__next_;
2689 __cn->__next_ = nullptr;
2690 --size();
Howard Hinnantb24c8022013-07-23 22:01:58 +00002691#if _LIBCPP_DEBUG_LEVEL >= 2
2692 __c_node* __c = __get_db()->__find_c_and_lock(this);
Eric Fiselier780b51d2016-12-28 05:53:01 +00002693 for (__i_node** __dp = __c->end_; __dp != __c->beg_; )
Howard Hinnantb24c8022013-07-23 22:01:58 +00002694 {
Eric Fiselier780b51d2016-12-28 05:53:01 +00002695 --__dp;
2696 iterator* __i = static_cast<iterator*>((*__dp)->__i_);
Howard Hinnantb24c8022013-07-23 22:01:58 +00002697 if (__i->__node_ == __cn)
2698 {
Eric Fiselier780b51d2016-12-28 05:53:01 +00002699 (*__dp)->__c_ = nullptr;
2700 if (--__c->end_ != __dp)
2701 memmove(__dp, __dp+1, (__c->end_ - __dp)*sizeof(__i_node*));
Howard Hinnantb24c8022013-07-23 22:01:58 +00002702 }
2703 }
2704 __get_db()->unlock();
2705#endif
Eric Fiselier40492ba2016-07-23 20:36:55 +00002706 return __node_holder(__cn->__upcast(), _Dp(__node_alloc(), true));
Howard Hinnant3e519522010-05-11 19:42:16 +00002707}
2708
2709template <class _Tp, class _Hash, class _Equal, class _Alloc>
2710template <class _Key>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00002711inline
Howard Hinnant3e519522010-05-11 19:42:16 +00002712typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2713__hash_table<_Tp, _Hash, _Equal, _Alloc>::__count_unique(const _Key& __k) const
2714{
2715 return static_cast<size_type>(find(__k) != end());
2716}
2717
2718template <class _Tp, class _Hash, class _Equal, class _Alloc>
2719template <class _Key>
2720typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2721__hash_table<_Tp, _Hash, _Equal, _Alloc>::__count_multi(const _Key& __k) const
2722{
2723 size_type __r = 0;
2724 const_iterator __i = find(__k);
2725 if (__i != end())
2726 {
2727 const_iterator __e = end();
2728 do
2729 {
2730 ++__i;
2731 ++__r;
2732 } while (__i != __e && key_eq()(*__i, __k));
2733 }
2734 return __r;
2735}
2736
2737template <class _Tp, class _Hash, class _Equal, class _Alloc>
2738template <class _Key>
2739pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator,
2740 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator>
2741__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_unique(
2742 const _Key& __k)
2743{
2744 iterator __i = find(__k);
2745 iterator __j = __i;
2746 if (__i != end())
2747 ++__j;
2748 return pair<iterator, iterator>(__i, __j);
2749}
2750
2751template <class _Tp, class _Hash, class _Equal, class _Alloc>
2752template <class _Key>
2753pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator,
2754 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator>
2755__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_unique(
2756 const _Key& __k) const
2757{
2758 const_iterator __i = find(__k);
2759 const_iterator __j = __i;
2760 if (__i != end())
2761 ++__j;
2762 return pair<const_iterator, const_iterator>(__i, __j);
2763}
2764
2765template <class _Tp, class _Hash, class _Equal, class _Alloc>
2766template <class _Key>
2767pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator,
2768 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator>
2769__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_multi(
2770 const _Key& __k)
2771{
2772 iterator __i = find(__k);
2773 iterator __j = __i;
2774 if (__i != end())
2775 {
2776 iterator __e = end();
2777 do
2778 {
2779 ++__j;
2780 } while (__j != __e && key_eq()(*__j, __k));
2781 }
2782 return pair<iterator, iterator>(__i, __j);
2783}
2784
2785template <class _Tp, class _Hash, class _Equal, class _Alloc>
2786template <class _Key>
2787pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator,
2788 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator>
2789__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_multi(
2790 const _Key& __k) const
2791{
2792 const_iterator __i = find(__k);
2793 const_iterator __j = __i;
2794 if (__i != end())
2795 {
2796 const_iterator __e = end();
2797 do
2798 {
2799 ++__j;
2800 } while (__j != __e && key_eq()(*__j, __k));
2801 }
2802 return pair<const_iterator, const_iterator>(__i, __j);
2803}
2804
2805template <class _Tp, class _Hash, class _Equal, class _Alloc>
2806void
2807__hash_table<_Tp, _Hash, _Equal, _Alloc>::swap(__hash_table& __u)
Eric Fiselier87a82492015-07-18 20:40:46 +00002808#if _LIBCPP_STD_VER <= 11
Eric Fiselier780b51d2016-12-28 05:53:01 +00002809 _NOEXCEPT_DEBUG_(
Marshall Clowe3fbe142015-07-13 20:04:56 +00002810 __is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value
Marshall Clowe3fbe142015-07-13 20:04:56 +00002811 && (!allocator_traits<__pointer_allocator>::propagate_on_container_swap::value
2812 || __is_nothrow_swappable<__pointer_allocator>::value)
2813 && (!__node_traits::propagate_on_container_swap::value
2814 || __is_nothrow_swappable<__node_allocator>::value)
Marshall Clowe3fbe142015-07-13 20:04:56 +00002815 )
Eric Fiselier87a82492015-07-18 20:40:46 +00002816#else
Eric Fiselier780b51d2016-12-28 05:53:01 +00002817 _NOEXCEPT_DEBUG_(__is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value)
Eric Fiselier87a82492015-07-18 20:40:46 +00002818#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002819{
Eric Fiselier780b51d2016-12-28 05:53:01 +00002820 _LIBCPP_ASSERT(__node_traits::propagate_on_container_swap::value ||
2821 this->__node_alloc() == __u.__node_alloc(),
2822 "list::swap: Either propagate_on_container_swap must be true"
2823 " or the allocators must compare equal");
Howard Hinnant3e519522010-05-11 19:42:16 +00002824 {
2825 __node_pointer_pointer __npp = __bucket_list_.release();
2826 __bucket_list_.reset(__u.__bucket_list_.release());
2827 __u.__bucket_list_.reset(__npp);
2828 }
Howard Hinnantce48a112011-06-30 21:18:19 +00002829 _VSTD::swap(__bucket_list_.get_deleter().size(), __u.__bucket_list_.get_deleter().size());
Marshall Clowe3fbe142015-07-13 20:04:56 +00002830 __swap_allocator(__bucket_list_.get_deleter().__alloc(),
Howard Hinnant3e519522010-05-11 19:42:16 +00002831 __u.__bucket_list_.get_deleter().__alloc());
Marshall Clowe3fbe142015-07-13 20:04:56 +00002832 __swap_allocator(__node_alloc(), __u.__node_alloc());
Howard Hinnantce48a112011-06-30 21:18:19 +00002833 _VSTD::swap(__p1_.first().__next_, __u.__p1_.first().__next_);
Howard Hinnant3e519522010-05-11 19:42:16 +00002834 __p2_.swap(__u.__p2_);
2835 __p3_.swap(__u.__p3_);
2836 if (size() > 0)
Eric Fiselier40492ba2016-07-23 20:36:55 +00002837 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash(), bucket_count())] =
2838 __p1_.first().__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00002839 if (__u.size() > 0)
Eric Fiselier40492ba2016-07-23 20:36:55 +00002840 __u.__bucket_list_[__constrain_hash(__u.__p1_.first().__next_->__hash(), __u.bucket_count())] =
2841 __u.__p1_.first().__ptr();
Howard Hinnantb24c8022013-07-23 22:01:58 +00002842#if _LIBCPP_DEBUG_LEVEL >= 2
2843 __get_db()->swap(this, &__u);
2844#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002845}
2846
2847template <class _Tp, class _Hash, class _Equal, class _Alloc>
2848typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2849__hash_table<_Tp, _Hash, _Equal, _Alloc>::bucket_size(size_type __n) const
2850{
Howard Hinnante5c13de2013-07-29 19:05:47 +00002851 _LIBCPP_ASSERT(__n < bucket_count(),
2852 "unordered container::bucket_size(n) called with n >= bucket_count()");
Eric Fiselier40492ba2016-07-23 20:36:55 +00002853 __next_pointer __np = __bucket_list_[__n];
Howard Hinnant3e519522010-05-11 19:42:16 +00002854 size_type __bc = bucket_count();
2855 size_type __r = 0;
2856 if (__np != nullptr)
2857 {
2858 for (__np = __np->__next_; __np != nullptr &&
Eric Fiselier40492ba2016-07-23 20:36:55 +00002859 __constrain_hash(__np->__hash(), __bc) == __n;
Howard Hinnant3e519522010-05-11 19:42:16 +00002860 __np = __np->__next_, ++__r)
2861 ;
2862 }
2863 return __r;
2864}
2865
Howard Hinnant37141072011-06-04 18:54:24 +00002866template <class _Tp, class _Hash, class _Equal, class _Alloc>
2867inline _LIBCPP_INLINE_VISIBILITY
2868void
2869swap(__hash_table<_Tp, _Hash, _Equal, _Alloc>& __x,
2870 __hash_table<_Tp, _Hash, _Equal, _Alloc>& __y)
2871 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
2872{
2873 __x.swap(__y);
2874}
2875
Howard Hinnantb24c8022013-07-23 22:01:58 +00002876#if _LIBCPP_DEBUG_LEVEL >= 2
2877
2878template <class _Tp, class _Hash, class _Equal, class _Alloc>
2879bool
2880__hash_table<_Tp, _Hash, _Equal, _Alloc>::__dereferenceable(const const_iterator* __i) const
2881{
2882 return __i->__node_ != nullptr;
2883}
2884
2885template <class _Tp, class _Hash, class _Equal, class _Alloc>
2886bool
2887__hash_table<_Tp, _Hash, _Equal, _Alloc>::__decrementable(const const_iterator*) const
2888{
2889 return false;
2890}
2891
2892template <class _Tp, class _Hash, class _Equal, class _Alloc>
2893bool
2894__hash_table<_Tp, _Hash, _Equal, _Alloc>::__addable(const const_iterator*, ptrdiff_t) const
2895{
2896 return false;
2897}
2898
2899template <class _Tp, class _Hash, class _Equal, class _Alloc>
2900bool
2901__hash_table<_Tp, _Hash, _Equal, _Alloc>::__subscriptable(const const_iterator*, ptrdiff_t) const
2902{
2903 return false;
2904}
2905
2906#endif // _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiseliera016efb2017-05-31 22:07:49 +00002907
Howard Hinnant3e519522010-05-11 19:42:16 +00002908_LIBCPP_END_NAMESPACE_STD
2909
Eric Fiseliera016efb2017-05-31 22:07:49 +00002910_LIBCPP_POP_MACROS
2911
Howard Hinnant3e519522010-05-11 19:42:16 +00002912#endif // _LIBCPP__HASH_TABLE