blob: e082e6c812f525c3bcc75cd50d688e7e4ff8c7a7 [file] [log] [blame]
Howard Hinnant3e519522010-05-11 19:42:16 +00001// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
Howard Hinnant5b08a8a2010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnant3e519522010-05-11 19:42:16 +00005//
Howard Hinnant412dbeb2010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnant3e519522010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP__HASH_TABLE
12#define _LIBCPP__HASH_TABLE
13
14#include <__config>
15#include <initializer_list>
16#include <memory>
17#include <iterator>
18#include <algorithm>
19#include <cmath>
Eric Fiselier75d0dcf2016-02-10 20:46:23 +000020#include <utility>
Howard Hinnant3e519522010-05-11 19:42:16 +000021
Howard Hinnantab4f4382011-11-29 16:45:27 +000022#include <__undef_min_max>
23
Eric Fiselierc1bd9192014-08-10 23:53:08 +000024#include <__debug>
Howard Hinnant42a30462013-08-02 00:26:35 +000025
Howard Hinnant073458b2011-10-17 20:05:10 +000026#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnant3e519522010-05-11 19:42:16 +000027#pragma GCC system_header
Howard Hinnant073458b2011-10-17 20:05:10 +000028#endif
Howard Hinnant3e519522010-05-11 19:42:16 +000029
30_LIBCPP_BEGIN_NAMESPACE_STD
31
Eric Fiselierfcd02212016-02-11 11:59:44 +000032
33#ifndef _LIBCPP_CXX03_LANG
34template <class _Key, class _Tp>
35union __hash_value_type;
36#else
37template <class _Key, class _Tp>
38struct __hash_value_type;
39#endif
40
41#ifndef _LIBCPP_CXX03_LANG
42template <class _Tp>
43struct __is_hash_value_type_imp : false_type {};
44
45template <class _Key, class _Value>
46struct __is_hash_value_type_imp<__hash_value_type<_Key, _Value>> : true_type {};
47
48template <class ..._Args>
49struct __is_hash_value_type : false_type {};
50
51template <class _One>
52struct __is_hash_value_type<_One> : __is_hash_value_type_imp<typename __uncvref<_One>::type> {};
53#endif
54
Howard Hinnant6e412562013-03-06 23:30:19 +000055_LIBCPP_FUNC_VIS
Howard Hinnantce534202011-06-14 19:58:17 +000056size_t __next_prime(size_t __n);
Howard Hinnant3e519522010-05-11 19:42:16 +000057
58template <class _NodePtr>
59struct __hash_node_base
60{
Eric Fiselier40492ba2016-07-23 20:36:55 +000061 typedef typename pointer_traits<_NodePtr>::element_type __node_type;
Howard Hinnant3e519522010-05-11 19:42:16 +000062 typedef __hash_node_base __first_node;
Eric Fiselier40492ba2016-07-23 20:36:55 +000063 typedef typename __rebind_pointer<_NodePtr, __first_node>::type __node_base_pointer;
64 typedef _NodePtr __node_pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +000065
Eric Fiselier40492ba2016-07-23 20:36:55 +000066#if defined(_LIBCPP_ABI_FIX_UNORDERED_NODE_POINTER_UB)
67 typedef __node_base_pointer __next_pointer;
68#else
69 typedef typename conditional<
70 is_pointer<__node_pointer>::value,
71 __node_base_pointer,
72 __node_pointer>::type __next_pointer;
73#endif
74
75 __next_pointer __next_;
76
77 _LIBCPP_INLINE_VISIBILITY
78 __next_pointer __ptr() _NOEXCEPT {
79 return static_cast<__next_pointer>(
80 pointer_traits<__node_base_pointer>::pointer_to(*this));
81 }
82
83 _LIBCPP_INLINE_VISIBILITY
84 __node_pointer __upcast() _NOEXCEPT {
85 return static_cast<__node_pointer>(
86 pointer_traits<__node_base_pointer>::pointer_to(*this));
87 }
88
89 _LIBCPP_INLINE_VISIBILITY
90 size_t __hash() const _NOEXCEPT {
91 return static_cast<__node_type const&>(*this).__hash_;
92 }
Howard Hinnant3e519522010-05-11 19:42:16 +000093
Howard Hinnant37141072011-06-04 18:54:24 +000094 _LIBCPP_INLINE_VISIBILITY __hash_node_base() _NOEXCEPT : __next_(nullptr) {}
Howard Hinnant3e519522010-05-11 19:42:16 +000095};
96
97template <class _Tp, class _VoidPtr>
98struct __hash_node
99 : public __hash_node_base
100 <
Eric Fiselier934b0922015-12-30 21:52:00 +0000101 typename __rebind_pointer<_VoidPtr, __hash_node<_Tp, _VoidPtr> >::type
Howard Hinnant3e519522010-05-11 19:42:16 +0000102 >
103{
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000104 typedef _Tp __node_value_type;
Howard Hinnant3e519522010-05-11 19:42:16 +0000105
Eric Fiselier40492ba2016-07-23 20:36:55 +0000106 size_t __hash_;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000107 __node_value_type __value_;
Howard Hinnant3e519522010-05-11 19:42:16 +0000108};
109
Howard Hinnant4cb38a82012-07-06 17:31:14 +0000110inline _LIBCPP_INLINE_VISIBILITY
111bool
Eric Fiselier9ba5c112015-02-02 21:31:48 +0000112__is_hash_power2(size_t __bc)
Howard Hinnant4cb38a82012-07-06 17:31:14 +0000113{
114 return __bc > 2 && !(__bc & (__bc - 1));
115}
116
117inline _LIBCPP_INLINE_VISIBILITY
118size_t
119__constrain_hash(size_t __h, size_t __bc)
120{
Eric Fiselier118cb412016-07-11 22:02:02 +0000121 return !(__bc & (__bc - 1)) ? __h & (__bc - 1) :
122 (__h < __bc ? __h : __h % __bc);
Howard Hinnant4cb38a82012-07-06 17:31:14 +0000123}
124
125inline _LIBCPP_INLINE_VISIBILITY
126size_t
Eric Fiselier9ba5c112015-02-02 21:31:48 +0000127__next_hash_pow2(size_t __n)
Howard Hinnant4cb38a82012-07-06 17:31:14 +0000128{
129 return size_t(1) << (std::numeric_limits<size_t>::digits - __clz(__n-1));
130}
131
Duncan P. N. Exon Smithfde79b42016-03-17 20:45:20 +0000132
Howard Hinnantce534202011-06-14 19:58:17 +0000133template <class _Tp, class _Hash, class _Equal, class _Alloc> class __hash_table;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000134
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +0000135template <class _NodePtr> class _LIBCPP_TEMPLATE_VIS __hash_iterator;
136template <class _ConstNodePtr> class _LIBCPP_TEMPLATE_VIS __hash_const_iterator;
137template <class _NodePtr> class _LIBCPP_TEMPLATE_VIS __hash_local_iterator;
138template <class _ConstNodePtr> class _LIBCPP_TEMPLATE_VIS __hash_const_local_iterator;
139template <class _HashIterator> class _LIBCPP_TEMPLATE_VIS __hash_map_iterator;
140template <class _HashIterator> class _LIBCPP_TEMPLATE_VIS __hash_map_const_iterator;
Howard Hinnant3e519522010-05-11 19:42:16 +0000141
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000142template <class _Tp>
Eric Fiselier43b121d2016-02-20 07:59:16 +0000143struct __hash_key_value_types {
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000144 static_assert(!is_reference<_Tp>::value && !is_const<_Tp>::value, "");
145 typedef _Tp key_type;
146 typedef _Tp __node_value_type;
147 typedef _Tp __container_value_type;
148 static const bool __is_map = false;
Eric Fiselierfcd02212016-02-11 11:59:44 +0000149
150 _LIBCPP_INLINE_VISIBILITY
151 static key_type const& __get_key(_Tp const& __v) {
152 return __v;
153 }
154 _LIBCPP_INLINE_VISIBILITY
155 static __container_value_type const& __get_value(__node_value_type const& __v) {
156 return __v;
157 }
158 _LIBCPP_INLINE_VISIBILITY
159 static __container_value_type* __get_ptr(__node_value_type& __n) {
160 return _VSTD::addressof(__n);
161 }
162#ifndef _LIBCPP_CXX03_LANG
163 _LIBCPP_INLINE_VISIBILITY
164 static __container_value_type&& __move(__node_value_type& __v) {
165 return _VSTD::move(__v);
166 }
167#endif
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000168};
169
170template <class _Key, class _Tp>
Eric Fiselier43b121d2016-02-20 07:59:16 +0000171struct __hash_key_value_types<__hash_value_type<_Key, _Tp> > {
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000172 typedef _Key key_type;
173 typedef _Tp mapped_type;
174 typedef __hash_value_type<_Key, _Tp> __node_value_type;
175 typedef pair<const _Key, _Tp> __container_value_type;
Eric Fiselierfcd02212016-02-11 11:59:44 +0000176 typedef pair<_Key, _Tp> __nc_value_type;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000177 typedef __container_value_type __map_value_type;
178 static const bool __is_map = true;
Eric Fiselierfcd02212016-02-11 11:59:44 +0000179
180 _LIBCPP_INLINE_VISIBILITY
181 static key_type const& __get_key(__container_value_type const& __v) {
182 return __v.first;
183 }
184
185 template <class _Up>
186 _LIBCPP_INLINE_VISIBILITY
187 static typename enable_if<__is_same_uncvref<_Up, __node_value_type>::value,
188 __container_value_type const&>::type
189 __get_value(_Up& __t) {
190 return __t.__cc;
191 }
192
193 template <class _Up>
194 _LIBCPP_INLINE_VISIBILITY
195 static typename enable_if<__is_same_uncvref<_Up, __container_value_type>::value,
196 __container_value_type const&>::type
197 __get_value(_Up& __t) {
198 return __t;
199 }
200
201 _LIBCPP_INLINE_VISIBILITY
202 static __container_value_type* __get_ptr(__node_value_type& __n) {
203 return _VSTD::addressof(__n.__cc);
204 }
205#ifndef _LIBCPP_CXX03_LANG
206 _LIBCPP_INLINE_VISIBILITY
207 static __nc_value_type&& __move(__node_value_type& __v) {
208 return _VSTD::move(__v.__nc);
209 }
210#endif
211
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000212};
213
Eric Fiselier43b121d2016-02-20 07:59:16 +0000214template <class _Tp, class _AllocPtr, class _KVTypes = __hash_key_value_types<_Tp>,
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000215 bool = _KVTypes::__is_map>
Eric Fiselier43b121d2016-02-20 07:59:16 +0000216struct __hash_map_pointer_types {};
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000217
218template <class _Tp, class _AllocPtr, class _KVTypes>
Eric Fiselier43b121d2016-02-20 07:59:16 +0000219struct __hash_map_pointer_types<_Tp, _AllocPtr, _KVTypes, true> {
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000220 typedef typename _KVTypes::__map_value_type _Mv;
221 typedef typename __rebind_pointer<_AllocPtr, _Mv>::type
222 __map_value_type_pointer;
223 typedef typename __rebind_pointer<_AllocPtr, const _Mv>::type
224 __const_map_value_type_pointer;
225};
226
227template <class _NodePtr, class _NodeT = typename pointer_traits<_NodePtr>::element_type>
228struct __hash_node_types;
229
230template <class _NodePtr, class _Tp, class _VoidPtr>
231struct __hash_node_types<_NodePtr, __hash_node<_Tp, _VoidPtr> >
Eric Fiselier43b121d2016-02-20 07:59:16 +0000232 : public __hash_key_value_types<_Tp>, __hash_map_pointer_types<_Tp, _VoidPtr>
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000233
234{
Eric Fiselier43b121d2016-02-20 07:59:16 +0000235 typedef __hash_key_value_types<_Tp> __base;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000236
237public:
238 typedef ptrdiff_t difference_type;
239 typedef size_t size_type;
240
241 typedef typename __rebind_pointer<_NodePtr, void>::type __void_pointer;
242
243 typedef typename pointer_traits<_NodePtr>::element_type __node_type;
244 typedef _NodePtr __node_pointer;
245
246 typedef __hash_node_base<__node_pointer> __node_base_type;
247 typedef typename __rebind_pointer<_NodePtr, __node_base_type>::type
248 __node_base_pointer;
249
Eric Fiselier40492ba2016-07-23 20:36:55 +0000250 typedef typename __node_base_type::__next_pointer __next_pointer;
251
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000252 typedef _Tp __node_value_type;
253 typedef typename __rebind_pointer<_VoidPtr, __node_value_type>::type
254 __node_value_type_pointer;
255 typedef typename __rebind_pointer<_VoidPtr, const __node_value_type>::type
256 __const_node_value_type_pointer;
Eric Fiselier40492ba2016-07-23 20:36:55 +0000257
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000258private:
259 static_assert(!is_const<__node_type>::value,
260 "_NodePtr should never be a pointer to const");
261 static_assert((is_same<typename pointer_traits<_VoidPtr>::element_type, void>::value),
262 "_VoidPtr does not point to unqualified void type");
263 static_assert((is_same<typename __rebind_pointer<_VoidPtr, __node_type>::type,
264 _NodePtr>::value), "_VoidPtr does not rebind to _NodePtr.");
265};
266
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000267template <class _HashIterator>
268struct __hash_node_types_from_iterator;
269template <class _NodePtr>
270struct __hash_node_types_from_iterator<__hash_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
271template <class _NodePtr>
272struct __hash_node_types_from_iterator<__hash_const_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
273template <class _NodePtr>
274struct __hash_node_types_from_iterator<__hash_local_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
275template <class _NodePtr>
276struct __hash_node_types_from_iterator<__hash_const_local_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
277
278
279template <class _NodeValueTp, class _VoidPtr>
280struct __make_hash_node_types {
281 typedef __hash_node<_NodeValueTp, _VoidPtr> _NodeTp;
282 typedef typename __rebind_pointer<_VoidPtr, _NodeTp>::type _NodePtr;
283 typedef __hash_node_types<_NodePtr> type;
284};
285
Howard Hinnant3e519522010-05-11 19:42:16 +0000286template <class _NodePtr>
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +0000287class _LIBCPP_TEMPLATE_VIS __hash_iterator
Howard Hinnant3e519522010-05-11 19:42:16 +0000288{
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000289 typedef __hash_node_types<_NodePtr> _NodeTypes;
Eric Fiselier40492ba2016-07-23 20:36:55 +0000290 typedef _NodePtr __node_pointer;
291 typedef typename _NodeTypes::__next_pointer __next_pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000292
Eric Fiselier40492ba2016-07-23 20:36:55 +0000293 __next_pointer __node_;
Howard Hinnant3e519522010-05-11 19:42:16 +0000294
295public:
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000296 typedef forward_iterator_tag iterator_category;
297 typedef typename _NodeTypes::__node_value_type value_type;
298 typedef typename _NodeTypes::difference_type difference_type;
299 typedef value_type& reference;
300 typedef typename _NodeTypes::__node_value_type_pointer pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000301
Eric Fiselier40492ba2016-07-23 20:36:55 +0000302 _LIBCPP_INLINE_VISIBILITY __hash_iterator() _NOEXCEPT : __node_(nullptr) {
303 _LIBCPP_DEBUG_MODE(__get_db()->__insert_i(this));
Howard Hinnantb24c8022013-07-23 22:01:58 +0000304 }
305
306#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant43d99232010-09-21 17:32:39 +0000307 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb24c8022013-07-23 22:01:58 +0000308 __hash_iterator(const __hash_iterator& __i)
309 : __node_(__i.__node_)
310 {
311 __get_db()->__iterator_copy(this, &__i);
312 }
313
Howard Hinnant43d99232010-09-21 17:32:39 +0000314 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb24c8022013-07-23 22:01:58 +0000315 ~__hash_iterator()
316 {
317 __get_db()->__erase_i(this);
318 }
319
320 _LIBCPP_INLINE_VISIBILITY
321 __hash_iterator& operator=(const __hash_iterator& __i)
322 {
323 if (this != &__i)
324 {
325 __get_db()->__iterator_copy(this, &__i);
326 __node_ = __i.__node_;
327 }
328 return *this;
329 }
Howard Hinnantb24c8022013-07-23 22:01:58 +0000330#endif // _LIBCPP_DEBUG_LEVEL >= 2
331
332 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000333 reference operator*() const {
334 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
335 "Attempted to dereference a non-dereferenceable unordered container iterator");
336 return __node_->__upcast()->__value_;
337 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000338
Howard Hinnant43d99232010-09-21 17:32:39 +0000339 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000340 pointer operator->() const {
341 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
342 "Attempted to dereference a non-dereferenceable unordered container iterator");
343 return pointer_traits<pointer>::pointer_to(__node_->__upcast()->__value_);
344 }
345
346 _LIBCPP_INLINE_VISIBILITY
347 __hash_iterator& operator++() {
348 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
Howard Hinnantb24c8022013-07-23 22:01:58 +0000349 "Attempted to increment non-incrementable unordered container iterator");
Howard Hinnant3e519522010-05-11 19:42:16 +0000350 __node_ = __node_->__next_;
351 return *this;
352 }
353
Howard Hinnant43d99232010-09-21 17:32:39 +0000354 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000355 __hash_iterator operator++(int)
356 {
357 __hash_iterator __t(*this);
358 ++(*this);
359 return __t;
360 }
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 {
Howard Hinnantb24c8022013-07-23 22:01:58 +0000365 return __x.__node_ == __y.__node_;
366 }
Howard Hinnant43d99232010-09-21 17:32:39 +0000367 friend _LIBCPP_INLINE_VISIBILITY
368 bool operator!=(const __hash_iterator& __x, const __hash_iterator& __y)
Howard Hinnantb24c8022013-07-23 22:01:58 +0000369 {return !(__x == __y);}
Howard Hinnant3e519522010-05-11 19:42:16 +0000370
371private:
Howard Hinnantb24c8022013-07-23 22:01:58 +0000372#if _LIBCPP_DEBUG_LEVEL >= 2
373 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000374 __hash_iterator(__next_pointer __node, const void* __c) _NOEXCEPT
Howard Hinnantb24c8022013-07-23 22:01:58 +0000375 : __node_(__node)
376 {
377 __get_db()->__insert_ic(this, __c);
378 }
379#else
Howard Hinnant43d99232010-09-21 17:32:39 +0000380 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000381 __hash_iterator(__next_pointer __node) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000382 : __node_(__node)
383 {}
Howard Hinnantb24c8022013-07-23 22:01:58 +0000384#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000385 template <class, class, class, class> friend class __hash_table;
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +0000386 template <class> friend class _LIBCPP_TEMPLATE_VIS __hash_const_iterator;
387 template <class> friend class _LIBCPP_TEMPLATE_VIS __hash_map_iterator;
388 template <class, class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS unordered_map;
389 template <class, class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS unordered_multimap;
Howard Hinnant3e519522010-05-11 19:42:16 +0000390};
391
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000392template <class _NodePtr>
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +0000393class _LIBCPP_TEMPLATE_VIS __hash_const_iterator
Howard Hinnant3e519522010-05-11 19:42:16 +0000394{
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000395 static_assert(!is_const<typename pointer_traits<_NodePtr>::element_type>::value, "");
396 typedef __hash_node_types<_NodePtr> _NodeTypes;
Eric Fiselier40492ba2016-07-23 20:36:55 +0000397 typedef _NodePtr __node_pointer;
398 typedef typename _NodeTypes::__next_pointer __next_pointer;
Eric Fiselier757373e2016-02-18 00:20:34 +0000399
Eric Fiselier40492ba2016-07-23 20:36:55 +0000400 __next_pointer __node_;
Howard Hinnant3e519522010-05-11 19:42:16 +0000401
402public:
Eric Fiselier757373e2016-02-18 00:20:34 +0000403 typedef __hash_iterator<_NodePtr> __non_const_iterator;
404
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000405 typedef forward_iterator_tag iterator_category;
406 typedef typename _NodeTypes::__node_value_type value_type;
407 typedef typename _NodeTypes::difference_type difference_type;
408 typedef const value_type& reference;
409 typedef typename _NodeTypes::__const_node_value_type_pointer pointer;
410
Howard Hinnant3e519522010-05-11 19:42:16 +0000411
Eric Fiselier40492ba2016-07-23 20:36:55 +0000412 _LIBCPP_INLINE_VISIBILITY __hash_const_iterator() _NOEXCEPT : __node_(nullptr) {
413 _LIBCPP_DEBUG_MODE(__get_db()->__insert_i(this));
Howard Hinnantb24c8022013-07-23 22:01:58 +0000414 }
Eric Fiselier40492ba2016-07-23 20:36:55 +0000415
Howard Hinnant43d99232010-09-21 17:32:39 +0000416 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +0000417 __hash_const_iterator(const __non_const_iterator& __x) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000418 : __node_(__x.__node_)
Howard Hinnantb24c8022013-07-23 22:01:58 +0000419 {
Eric Fiselier40492ba2016-07-23 20:36:55 +0000420 _LIBCPP_DEBUG_MODE(__get_db()->__iterator_copy(this, &__x));
Howard Hinnantb24c8022013-07-23 22:01:58 +0000421 }
422
423#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant43d99232010-09-21 17:32:39 +0000424 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb24c8022013-07-23 22:01:58 +0000425 __hash_const_iterator(const __hash_const_iterator& __i)
426 : __node_(__i.__node_)
427 {
428 __get_db()->__iterator_copy(this, &__i);
429 }
430
Howard Hinnant43d99232010-09-21 17:32:39 +0000431 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb24c8022013-07-23 22:01:58 +0000432 ~__hash_const_iterator()
433 {
434 __get_db()->__erase_i(this);
435 }
436
437 _LIBCPP_INLINE_VISIBILITY
438 __hash_const_iterator& operator=(const __hash_const_iterator& __i)
439 {
440 if (this != &__i)
441 {
442 __get_db()->__iterator_copy(this, &__i);
443 __node_ = __i.__node_;
444 }
445 return *this;
446 }
Howard Hinnantb24c8022013-07-23 22:01:58 +0000447#endif // _LIBCPP_DEBUG_LEVEL >= 2
448
449 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000450 reference operator*() const {
451 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
Howard Hinnantb24c8022013-07-23 22:01:58 +0000452 "Attempted to dereference a non-dereferenceable unordered container const_iterator");
Eric Fiselier40492ba2016-07-23 20:36:55 +0000453 return __node_->__upcast()->__value_;
454 }
Howard Hinnantb24c8022013-07-23 22:01:58 +0000455 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000456 pointer operator->() const {
457 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
Howard Hinnantb24c8022013-07-23 22:01:58 +0000458 "Attempted to dereference a non-dereferenceable unordered container const_iterator");
Eric Fiselier40492ba2016-07-23 20:36:55 +0000459 return pointer_traits<pointer>::pointer_to(__node_->__upcast()->__value_);
460 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000461
Howard Hinnant43d99232010-09-21 17:32:39 +0000462 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000463 __hash_const_iterator& operator++() {
464 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
465 "Attempted to increment non-incrementable unordered container const_iterator");
Howard Hinnant3e519522010-05-11 19:42:16 +0000466 __node_ = __node_->__next_;
467 return *this;
468 }
469
Howard Hinnant43d99232010-09-21 17:32:39 +0000470 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000471 __hash_const_iterator operator++(int)
472 {
473 __hash_const_iterator __t(*this);
474 ++(*this);
475 return __t;
476 }
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 {
Howard Hinnantb24c8022013-07-23 22:01:58 +0000481 return __x.__node_ == __y.__node_;
482 }
Howard Hinnant43d99232010-09-21 17:32:39 +0000483 friend _LIBCPP_INLINE_VISIBILITY
484 bool operator!=(const __hash_const_iterator& __x, const __hash_const_iterator& __y)
Howard Hinnantb24c8022013-07-23 22:01:58 +0000485 {return !(__x == __y);}
Howard Hinnant3e519522010-05-11 19:42:16 +0000486
487private:
Howard Hinnantb24c8022013-07-23 22:01:58 +0000488#if _LIBCPP_DEBUG_LEVEL >= 2
489 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000490 __hash_const_iterator(__next_pointer __node, const void* __c) _NOEXCEPT
Howard Hinnantb24c8022013-07-23 22:01:58 +0000491 : __node_(__node)
492 {
493 __get_db()->__insert_ic(this, __c);
494 }
495#else
Howard Hinnant43d99232010-09-21 17:32:39 +0000496 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000497 __hash_const_iterator(__next_pointer __node) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000498 : __node_(__node)
499 {}
Howard Hinnantb24c8022013-07-23 22:01:58 +0000500#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000501 template <class, class, class, class> friend class __hash_table;
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +0000502 template <class> friend class _LIBCPP_TEMPLATE_VIS __hash_map_const_iterator;
503 template <class, class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS unordered_map;
504 template <class, class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS unordered_multimap;
Howard Hinnant3e519522010-05-11 19:42:16 +0000505};
506
Howard Hinnant3e519522010-05-11 19:42:16 +0000507template <class _NodePtr>
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +0000508class _LIBCPP_TEMPLATE_VIS __hash_local_iterator
Howard Hinnant3e519522010-05-11 19:42:16 +0000509{
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000510 typedef __hash_node_types<_NodePtr> _NodeTypes;
Eric Fiselier40492ba2016-07-23 20:36:55 +0000511 typedef _NodePtr __node_pointer;
512 typedef typename _NodeTypes::__next_pointer __next_pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000513
Eric Fiselier40492ba2016-07-23 20:36:55 +0000514 __next_pointer __node_;
Howard Hinnant3e519522010-05-11 19:42:16 +0000515 size_t __bucket_;
516 size_t __bucket_count_;
517
Howard Hinnant3e519522010-05-11 19:42:16 +0000518public:
519 typedef forward_iterator_tag iterator_category;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000520 typedef typename _NodeTypes::__node_value_type value_type;
521 typedef typename _NodeTypes::difference_type difference_type;
Howard Hinnant3e519522010-05-11 19:42:16 +0000522 typedef value_type& reference;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000523 typedef typename _NodeTypes::__node_value_type_pointer pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000524
Eric Fiselier40492ba2016-07-23 20:36:55 +0000525 _LIBCPP_INLINE_VISIBILITY __hash_local_iterator() _NOEXCEPT : __node_(nullptr) {
526 _LIBCPP_DEBUG_MODE(__get_db()->__insert_i(this));
Howard Hinnantb24c8022013-07-23 22:01:58 +0000527 }
528
529#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant43d99232010-09-21 17:32:39 +0000530 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb24c8022013-07-23 22:01:58 +0000531 __hash_local_iterator(const __hash_local_iterator& __i)
532 : __node_(__i.__node_),
533 __bucket_(__i.__bucket_),
534 __bucket_count_(__i.__bucket_count_)
535 {
536 __get_db()->__iterator_copy(this, &__i);
537 }
538
Howard Hinnant43d99232010-09-21 17:32:39 +0000539 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb24c8022013-07-23 22:01:58 +0000540 ~__hash_local_iterator()
541 {
542 __get_db()->__erase_i(this);
543 }
544
545 _LIBCPP_INLINE_VISIBILITY
546 __hash_local_iterator& operator=(const __hash_local_iterator& __i)
547 {
548 if (this != &__i)
549 {
550 __get_db()->__iterator_copy(this, &__i);
551 __node_ = __i.__node_;
552 __bucket_ = __i.__bucket_;
553 __bucket_count_ = __i.__bucket_count_;
554 }
555 return *this;
556 }
Howard Hinnantb24c8022013-07-23 22:01:58 +0000557#endif // _LIBCPP_DEBUG_LEVEL >= 2
558
559 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000560 reference operator*() const {
561 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
Howard Hinnantb24c8022013-07-23 22:01:58 +0000562 "Attempted to dereference a non-dereferenceable unordered container local_iterator");
Eric Fiselier40492ba2016-07-23 20:36:55 +0000563 return __node_->__upcast()->__value_;
564 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000565
Howard Hinnant43d99232010-09-21 17:32:39 +0000566 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000567 pointer operator->() const {
568 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
569 "Attempted to dereference a non-dereferenceable unordered container local_iterator");
570 return pointer_traits<pointer>::pointer_to(__node_->__upcast()->__value_);
571 }
572
573 _LIBCPP_INLINE_VISIBILITY
574 __hash_local_iterator& operator++() {
575 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
Howard Hinnantb24c8022013-07-23 22:01:58 +0000576 "Attempted to increment non-incrementable unordered container local_iterator");
Howard Hinnant3e519522010-05-11 19:42:16 +0000577 __node_ = __node_->__next_;
Eric Fiselier40492ba2016-07-23 20:36:55 +0000578 if (__node_ != nullptr && __constrain_hash(__node_->__hash(), __bucket_count_) != __bucket_)
Howard Hinnant3e519522010-05-11 19:42:16 +0000579 __node_ = nullptr;
580 return *this;
581 }
582
Howard Hinnant43d99232010-09-21 17:32:39 +0000583 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000584 __hash_local_iterator operator++(int)
585 {
586 __hash_local_iterator __t(*this);
587 ++(*this);
588 return __t;
589 }
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 {
Howard Hinnantb24c8022013-07-23 22:01:58 +0000594 return __x.__node_ == __y.__node_;
595 }
Howard Hinnant43d99232010-09-21 17:32:39 +0000596 friend _LIBCPP_INLINE_VISIBILITY
597 bool operator!=(const __hash_local_iterator& __x, const __hash_local_iterator& __y)
Howard Hinnantb24c8022013-07-23 22:01:58 +0000598 {return !(__x == __y);}
Howard Hinnant3e519522010-05-11 19:42:16 +0000599
600private:
Howard Hinnantb24c8022013-07-23 22:01:58 +0000601#if _LIBCPP_DEBUG_LEVEL >= 2
602 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000603 __hash_local_iterator(__next_pointer __node, size_t __bucket,
Howard Hinnantb24c8022013-07-23 22:01:58 +0000604 size_t __bucket_count, const void* __c) _NOEXCEPT
605 : __node_(__node),
606 __bucket_(__bucket),
607 __bucket_count_(__bucket_count)
608 {
609 __get_db()->__insert_ic(this, __c);
610 if (__node_ != nullptr)
611 __node_ = __node_->__next_;
612 }
613#else
Howard Hinnant43d99232010-09-21 17:32:39 +0000614 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000615 __hash_local_iterator(__next_pointer __node, size_t __bucket,
Howard Hinnant37141072011-06-04 18:54:24 +0000616 size_t __bucket_count) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000617 : __node_(__node),
618 __bucket_(__bucket),
619 __bucket_count_(__bucket_count)
620 {
621 if (__node_ != nullptr)
622 __node_ = __node_->__next_;
623 }
Howard Hinnantb24c8022013-07-23 22:01:58 +0000624#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000625 template <class, class, class, class> friend class __hash_table;
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +0000626 template <class> friend class _LIBCPP_TEMPLATE_VIS __hash_const_local_iterator;
627 template <class> friend class _LIBCPP_TEMPLATE_VIS __hash_map_iterator;
Howard Hinnant3e519522010-05-11 19:42:16 +0000628};
629
630template <class _ConstNodePtr>
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +0000631class _LIBCPP_TEMPLATE_VIS __hash_const_local_iterator
Howard Hinnant3e519522010-05-11 19:42:16 +0000632{
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000633 typedef __hash_node_types<_ConstNodePtr> _NodeTypes;
Eric Fiselier40492ba2016-07-23 20:36:55 +0000634 typedef _ConstNodePtr __node_pointer;
635 typedef typename _NodeTypes::__next_pointer __next_pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000636
Eric Fiselier40492ba2016-07-23 20:36:55 +0000637 __next_pointer __node_;
Howard Hinnant3e519522010-05-11 19:42:16 +0000638 size_t __bucket_;
639 size_t __bucket_count_;
640
641 typedef pointer_traits<__node_pointer> __pointer_traits;
642 typedef typename __pointer_traits::element_type __node;
643 typedef typename remove_const<__node>::type __non_const_node;
Eric Fiselier934b0922015-12-30 21:52:00 +0000644 typedef typename __rebind_pointer<__node_pointer, __non_const_node>::type
645 __non_const_node_pointer;
Eric Fiselier757373e2016-02-18 00:20:34 +0000646public:
Howard Hinnant3e519522010-05-11 19:42:16 +0000647 typedef __hash_local_iterator<__non_const_node_pointer>
648 __non_const_iterator;
Eric Fiselier757373e2016-02-18 00:20:34 +0000649
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000650 typedef forward_iterator_tag iterator_category;
651 typedef typename _NodeTypes::__node_value_type value_type;
652 typedef typename _NodeTypes::difference_type difference_type;
653 typedef const value_type& reference;
654 typedef typename _NodeTypes::__const_node_value_type_pointer pointer;
Eric Fiselier934b0922015-12-30 21:52:00 +0000655
Howard Hinnant3e519522010-05-11 19:42:16 +0000656
Eric Fiselier40492ba2016-07-23 20:36:55 +0000657 _LIBCPP_INLINE_VISIBILITY __hash_const_local_iterator() _NOEXCEPT : __node_(nullptr) {
658 _LIBCPP_DEBUG_MODE(__get_db()->__insert_i(this));
Howard Hinnantb24c8022013-07-23 22:01:58 +0000659 }
660
Howard Hinnant43d99232010-09-21 17:32:39 +0000661 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +0000662 __hash_const_local_iterator(const __non_const_iterator& __x) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000663 : __node_(__x.__node_),
664 __bucket_(__x.__bucket_),
665 __bucket_count_(__x.__bucket_count_)
Howard Hinnantb24c8022013-07-23 22:01:58 +0000666 {
Eric Fiselier40492ba2016-07-23 20:36:55 +0000667 _LIBCPP_DEBUG_MODE(__get_db()->__iterator_copy(this, &__x));
Howard Hinnantb24c8022013-07-23 22:01:58 +0000668 }
669
670#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant43d99232010-09-21 17:32:39 +0000671 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb24c8022013-07-23 22:01:58 +0000672 __hash_const_local_iterator(const __hash_const_local_iterator& __i)
673 : __node_(__i.__node_),
674 __bucket_(__i.__bucket_),
675 __bucket_count_(__i.__bucket_count_)
676 {
677 __get_db()->__iterator_copy(this, &__i);
678 }
679
Howard Hinnant43d99232010-09-21 17:32:39 +0000680 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb24c8022013-07-23 22:01:58 +0000681 ~__hash_const_local_iterator()
682 {
683 __get_db()->__erase_i(this);
684 }
685
686 _LIBCPP_INLINE_VISIBILITY
687 __hash_const_local_iterator& operator=(const __hash_const_local_iterator& __i)
688 {
689 if (this != &__i)
690 {
691 __get_db()->__iterator_copy(this, &__i);
692 __node_ = __i.__node_;
693 __bucket_ = __i.__bucket_;
694 __bucket_count_ = __i.__bucket_count_;
695 }
696 return *this;
697 }
Howard Hinnantb24c8022013-07-23 22:01:58 +0000698#endif // _LIBCPP_DEBUG_LEVEL >= 2
699
700 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000701 reference operator*() const {
702 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
Howard Hinnantb24c8022013-07-23 22:01:58 +0000703 "Attempted to dereference a non-dereferenceable unordered container const_local_iterator");
Eric Fiselier40492ba2016-07-23 20:36:55 +0000704 return __node_->__upcast()->__value_;
705 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000706
Howard Hinnant43d99232010-09-21 17:32:39 +0000707 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000708 pointer operator->() const {
709 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
710 "Attempted to dereference a non-dereferenceable unordered container const_local_iterator");
711 return pointer_traits<pointer>::pointer_to(__node_->__upcast()->__value_);
712 }
713
714 _LIBCPP_INLINE_VISIBILITY
715 __hash_const_local_iterator& operator++() {
716 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
Howard Hinnantb24c8022013-07-23 22:01:58 +0000717 "Attempted to increment non-incrementable unordered container const_local_iterator");
Howard Hinnant3e519522010-05-11 19:42:16 +0000718 __node_ = __node_->__next_;
Eric Fiselier40492ba2016-07-23 20:36:55 +0000719 if (__node_ != nullptr && __constrain_hash(__node_->__hash(), __bucket_count_) != __bucket_)
Howard Hinnant3e519522010-05-11 19:42:16 +0000720 __node_ = nullptr;
721 return *this;
722 }
723
Howard Hinnant43d99232010-09-21 17:32:39 +0000724 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000725 __hash_const_local_iterator operator++(int)
726 {
727 __hash_const_local_iterator __t(*this);
728 ++(*this);
729 return __t;
730 }
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 {
Howard Hinnantb24c8022013-07-23 22:01:58 +0000735 return __x.__node_ == __y.__node_;
736 }
Howard Hinnant43d99232010-09-21 17:32:39 +0000737 friend _LIBCPP_INLINE_VISIBILITY
738 bool operator!=(const __hash_const_local_iterator& __x, const __hash_const_local_iterator& __y)
Howard Hinnantb24c8022013-07-23 22:01:58 +0000739 {return !(__x == __y);}
Howard Hinnant3e519522010-05-11 19:42:16 +0000740
741private:
Howard Hinnantb24c8022013-07-23 22:01:58 +0000742#if _LIBCPP_DEBUG_LEVEL >= 2
743 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000744 __hash_const_local_iterator(__next_pointer __node, size_t __bucket,
Howard Hinnantb24c8022013-07-23 22:01:58 +0000745 size_t __bucket_count, const void* __c) _NOEXCEPT
746 : __node_(__node),
747 __bucket_(__bucket),
748 __bucket_count_(__bucket_count)
749 {
750 __get_db()->__insert_ic(this, __c);
751 if (__node_ != nullptr)
752 __node_ = __node_->__next_;
753 }
754#else
Howard Hinnant43d99232010-09-21 17:32:39 +0000755 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000756 __hash_const_local_iterator(__next_pointer __node, size_t __bucket,
Howard Hinnant37141072011-06-04 18:54:24 +0000757 size_t __bucket_count) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000758 : __node_(__node),
759 __bucket_(__bucket),
760 __bucket_count_(__bucket_count)
761 {
762 if (__node_ != nullptr)
763 __node_ = __node_->__next_;
764 }
Howard Hinnantb24c8022013-07-23 22:01:58 +0000765#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000766 template <class, class, class, class> friend class __hash_table;
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +0000767 template <class> friend class _LIBCPP_TEMPLATE_VIS __hash_map_const_iterator;
Howard Hinnant3e519522010-05-11 19:42:16 +0000768};
769
770template <class _Alloc>
771class __bucket_list_deallocator
772{
773 typedef _Alloc allocator_type;
774 typedef allocator_traits<allocator_type> __alloc_traits;
775 typedef typename __alloc_traits::size_type size_type;
776
777 __compressed_pair<size_type, allocator_type> __data_;
778public:
779 typedef typename __alloc_traits::pointer pointer;
780
Howard Hinnant43d99232010-09-21 17:32:39 +0000781 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000782 __bucket_list_deallocator()
Howard Hinnant37141072011-06-04 18:54:24 +0000783 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +0000784 : __data_(0) {}
Howard Hinnant43d99232010-09-21 17:32:39 +0000785
786 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000787 __bucket_list_deallocator(const allocator_type& __a, size_type __size)
Howard Hinnant37141072011-06-04 18:54:24 +0000788 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +0000789 : __data_(__size, __a) {}
790
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000791#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +0000792
Howard Hinnant43d99232010-09-21 17:32:39 +0000793 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000794 __bucket_list_deallocator(__bucket_list_deallocator&& __x)
Howard Hinnant37141072011-06-04 18:54:24 +0000795 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnantce48a112011-06-30 21:18:19 +0000796 : __data_(_VSTD::move(__x.__data_))
Howard Hinnant3e519522010-05-11 19:42:16 +0000797 {
798 __x.size() = 0;
799 }
800
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000801#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +0000802
Howard Hinnant37141072011-06-04 18:54:24 +0000803 _LIBCPP_INLINE_VISIBILITY
804 size_type& size() _NOEXCEPT {return __data_.first();}
805 _LIBCPP_INLINE_VISIBILITY
806 size_type size() const _NOEXCEPT {return __data_.first();}
Howard Hinnant3e519522010-05-11 19:42:16 +0000807
Howard Hinnant43d99232010-09-21 17:32:39 +0000808 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +0000809 allocator_type& __alloc() _NOEXCEPT {return __data_.second();}
810 _LIBCPP_INLINE_VISIBILITY
811 const allocator_type& __alloc() const _NOEXCEPT {return __data_.second();}
812
813 _LIBCPP_INLINE_VISIBILITY
814 void operator()(pointer __p) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000815 {
816 __alloc_traits::deallocate(__alloc(), __p, size());
817 }
818};
819
Howard Hinnantce534202011-06-14 19:58:17 +0000820template <class _Alloc> class __hash_map_node_destructor;
Howard Hinnant3e519522010-05-11 19:42:16 +0000821
822template <class _Alloc>
823class __hash_node_destructor
824{
825 typedef _Alloc allocator_type;
826 typedef allocator_traits<allocator_type> __alloc_traits;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000827
Howard Hinnant3e519522010-05-11 19:42:16 +0000828public:
829 typedef typename __alloc_traits::pointer pointer;
830private:
Eric Fiselierfcd02212016-02-11 11:59:44 +0000831 typedef __hash_node_types<pointer> _NodeTypes;
Howard Hinnant3e519522010-05-11 19:42:16 +0000832
833 allocator_type& __na_;
834
835 __hash_node_destructor& operator=(const __hash_node_destructor&);
836
837public:
838 bool __value_constructed;
839
Howard Hinnant43d99232010-09-21 17:32:39 +0000840 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf622b582011-07-31 17:04:30 +0000841 explicit __hash_node_destructor(allocator_type& __na,
842 bool __constructed = false) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000843 : __na_(__na),
Howard Hinnantf622b582011-07-31 17:04:30 +0000844 __value_constructed(__constructed)
Howard Hinnant3e519522010-05-11 19:42:16 +0000845 {}
846
Howard Hinnant43d99232010-09-21 17:32:39 +0000847 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +0000848 void operator()(pointer __p) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000849 {
850 if (__value_constructed)
Eric Fiselierfcd02212016-02-11 11:59:44 +0000851 __alloc_traits::destroy(__na_, _NodeTypes::__get_ptr(__p->__value_));
Howard Hinnant3e519522010-05-11 19:42:16 +0000852 if (__p)
853 __alloc_traits::deallocate(__na_, __p, 1);
854 }
855
856 template <class> friend class __hash_map_node_destructor;
857};
858
859template <class _Tp, class _Hash, class _Equal, class _Alloc>
860class __hash_table
861{
862public:
863 typedef _Tp value_type;
864 typedef _Hash hasher;
865 typedef _Equal key_equal;
866 typedef _Alloc allocator_type;
867
868private:
869 typedef allocator_traits<allocator_type> __alloc_traits;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000870 typedef typename
871 __make_hash_node_types<value_type, typename __alloc_traits::void_pointer>::type
872 _NodeTypes;
Howard Hinnant3e519522010-05-11 19:42:16 +0000873public:
Eric Fiselierfcd02212016-02-11 11:59:44 +0000874
875 typedef typename _NodeTypes::__node_value_type __node_value_type;
876 typedef typename _NodeTypes::__container_value_type __container_value_type;
Duncan P. N. Exon Smithfde79b42016-03-17 20:45:20 +0000877 typedef typename _NodeTypes::key_type key_type;
Howard Hinnant3e519522010-05-11 19:42:16 +0000878 typedef value_type& reference;
879 typedef const value_type& const_reference;
880 typedef typename __alloc_traits::pointer pointer;
881 typedef typename __alloc_traits::const_pointer const_pointer;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000882#ifndef _LIBCPP_ABI_FIX_UNORDERED_CONTAINER_SIZE_TYPE
Howard Hinnant3e519522010-05-11 19:42:16 +0000883 typedef typename __alloc_traits::size_type size_type;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000884#else
885 typedef typename _NodeTypes::size_type size_type;
886#endif
887 typedef typename _NodeTypes::difference_type difference_type;
Howard Hinnant3e519522010-05-11 19:42:16 +0000888public:
889 // Create __node
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000890
891 typedef typename _NodeTypes::__node_type __node;
Marshall Clow1f508012015-04-07 05:21:38 +0000892 typedef typename __rebind_alloc_helper<__alloc_traits, __node>::type __node_allocator;
Howard Hinnant3e519522010-05-11 19:42:16 +0000893 typedef allocator_traits<__node_allocator> __node_traits;
Eric Fiselier8e397682016-02-11 15:22:37 +0000894 typedef typename _NodeTypes::__void_pointer __void_pointer;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000895 typedef typename _NodeTypes::__node_pointer __node_pointer;
896 typedef typename _NodeTypes::__node_pointer __node_const_pointer;
897 typedef typename _NodeTypes::__node_base_type __first_node;
898 typedef typename _NodeTypes::__node_base_pointer __node_base_pointer;
Eric Fiselier40492ba2016-07-23 20:36:55 +0000899 typedef typename _NodeTypes::__next_pointer __next_pointer;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000900
901private:
902 // check for sane allocator pointer rebinding semantics. Rebinding the
903 // allocator for a new pointer type should be exactly the same as rebinding
904 // the pointer using 'pointer_traits'.
905 static_assert((is_same<__node_pointer, typename __node_traits::pointer>::value),
906 "Allocator does not rebind pointers in a sane manner.");
907 typedef typename __rebind_alloc_helper<__node_traits, __first_node>::type
908 __node_base_allocator;
909 typedef allocator_traits<__node_base_allocator> __node_base_traits;
910 static_assert((is_same<__node_base_pointer, typename __node_base_traits::pointer>::value),
911 "Allocator does not rebind pointers in a sane manner.");
Howard Hinnant3e519522010-05-11 19:42:16 +0000912
913private:
914
Eric Fiselier40492ba2016-07-23 20:36:55 +0000915 typedef typename __rebind_alloc_helper<__node_traits, __next_pointer>::type __pointer_allocator;
Howard Hinnant3e519522010-05-11 19:42:16 +0000916 typedef __bucket_list_deallocator<__pointer_allocator> __bucket_list_deleter;
Eric Fiselier40492ba2016-07-23 20:36:55 +0000917 typedef unique_ptr<__next_pointer[], __bucket_list_deleter> __bucket_list;
Howard Hinnant3e519522010-05-11 19:42:16 +0000918 typedef allocator_traits<__pointer_allocator> __pointer_alloc_traits;
Eric Fiselier40492ba2016-07-23 20:36:55 +0000919 typedef typename __bucket_list_deleter::pointer __node_pointer_pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000920
921 // --- Member data begin ---
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000922 __bucket_list __bucket_list_;
923 __compressed_pair<__first_node, __node_allocator> __p1_;
924 __compressed_pair<size_type, hasher> __p2_;
925 __compressed_pair<float, key_equal> __p3_;
Howard Hinnant3e519522010-05-11 19:42:16 +0000926 // --- Member data end ---
927
Howard Hinnant37141072011-06-04 18:54:24 +0000928 _LIBCPP_INLINE_VISIBILITY
929 size_type& size() _NOEXCEPT {return __p2_.first();}
Howard Hinnant3e519522010-05-11 19:42:16 +0000930public:
Howard Hinnant37141072011-06-04 18:54:24 +0000931 _LIBCPP_INLINE_VISIBILITY
932 size_type size() const _NOEXCEPT {return __p2_.first();}
Howard Hinnant3e519522010-05-11 19:42:16 +0000933
Howard Hinnant37141072011-06-04 18:54:24 +0000934 _LIBCPP_INLINE_VISIBILITY
935 hasher& hash_function() _NOEXCEPT {return __p2_.second();}
936 _LIBCPP_INLINE_VISIBILITY
937 const hasher& hash_function() const _NOEXCEPT {return __p2_.second();}
Howard Hinnant3e519522010-05-11 19:42:16 +0000938
Howard Hinnant37141072011-06-04 18:54:24 +0000939 _LIBCPP_INLINE_VISIBILITY
940 float& max_load_factor() _NOEXCEPT {return __p3_.first();}
941 _LIBCPP_INLINE_VISIBILITY
942 float max_load_factor() const _NOEXCEPT {return __p3_.first();}
Howard Hinnant3e519522010-05-11 19:42:16 +0000943
Howard Hinnant37141072011-06-04 18:54:24 +0000944 _LIBCPP_INLINE_VISIBILITY
945 key_equal& key_eq() _NOEXCEPT {return __p3_.second();}
946 _LIBCPP_INLINE_VISIBILITY
947 const key_equal& key_eq() const _NOEXCEPT {return __p3_.second();}
Howard Hinnant3e519522010-05-11 19:42:16 +0000948
Howard Hinnant37141072011-06-04 18:54:24 +0000949 _LIBCPP_INLINE_VISIBILITY
950 __node_allocator& __node_alloc() _NOEXCEPT {return __p1_.second();}
951 _LIBCPP_INLINE_VISIBILITY
952 const __node_allocator& __node_alloc() const _NOEXCEPT
953 {return __p1_.second();}
Howard Hinnant3e519522010-05-11 19:42:16 +0000954
955public:
956 typedef __hash_iterator<__node_pointer> iterator;
Howard Hinnant307f8142013-06-22 15:21:29 +0000957 typedef __hash_const_iterator<__node_pointer> const_iterator;
Howard Hinnant3e519522010-05-11 19:42:16 +0000958 typedef __hash_local_iterator<__node_pointer> local_iterator;
Howard Hinnant307f8142013-06-22 15:21:29 +0000959 typedef __hash_const_local_iterator<__node_pointer> const_local_iterator;
Howard Hinnant3e519522010-05-11 19:42:16 +0000960
Evgeniy Stepanov906c8722015-11-07 01:22:13 +0000961 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +0000962 __hash_table()
963 _NOEXCEPT_(
964 is_nothrow_default_constructible<__bucket_list>::value &&
965 is_nothrow_default_constructible<__first_node>::value &&
966 is_nothrow_default_constructible<__node_allocator>::value &&
967 is_nothrow_default_constructible<hasher>::value &&
968 is_nothrow_default_constructible<key_equal>::value);
Evgeniy Stepanov906c8722015-11-07 01:22:13 +0000969 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000970 __hash_table(const hasher& __hf, const key_equal& __eql);
971 __hash_table(const hasher& __hf, const key_equal& __eql,
972 const allocator_type& __a);
973 explicit __hash_table(const allocator_type& __a);
974 __hash_table(const __hash_table& __u);
975 __hash_table(const __hash_table& __u, const allocator_type& __a);
Eric Fiselierfcd02212016-02-11 11:59:44 +0000976#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant37141072011-06-04 18:54:24 +0000977 __hash_table(__hash_table&& __u)
978 _NOEXCEPT_(
979 is_nothrow_move_constructible<__bucket_list>::value &&
980 is_nothrow_move_constructible<__first_node>::value &&
981 is_nothrow_move_constructible<__node_allocator>::value &&
982 is_nothrow_move_constructible<hasher>::value &&
983 is_nothrow_move_constructible<key_equal>::value);
Howard Hinnant3e519522010-05-11 19:42:16 +0000984 __hash_table(__hash_table&& __u, const allocator_type& __a);
Eric Fiselierfcd02212016-02-11 11:59:44 +0000985#endif // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +0000986 ~__hash_table();
987
988 __hash_table& operator=(const __hash_table& __u);
Eric Fiselierfcd02212016-02-11 11:59:44 +0000989#ifndef _LIBCPP_CXX03_LANG
Evgeniy Stepanov906c8722015-11-07 01:22:13 +0000990 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +0000991 __hash_table& operator=(__hash_table&& __u)
992 _NOEXCEPT_(
993 __node_traits::propagate_on_container_move_assignment::value &&
994 is_nothrow_move_assignable<__node_allocator>::value &&
995 is_nothrow_move_assignable<hasher>::value &&
996 is_nothrow_move_assignable<key_equal>::value);
Howard Hinnant3e519522010-05-11 19:42:16 +0000997#endif
998 template <class _InputIterator>
999 void __assign_unique(_InputIterator __first, _InputIterator __last);
1000 template <class _InputIterator>
1001 void __assign_multi(_InputIterator __first, _InputIterator __last);
1002
Howard Hinnant43d99232010-09-21 17:32:39 +00001003 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001004 size_type max_size() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001005 {
Eric Fiselier55b31b4e2016-11-23 01:18:56 +00001006 return std::min<size_type>(
Eric Fiselier341c9dd2016-11-23 09:16:12 +00001007 __node_traits::max_size(__node_alloc()),
Eric Fiselier55b31b4e2016-11-23 01:18:56 +00001008 numeric_limits<difference_type >::max()
1009 );
Howard Hinnant3e519522010-05-11 19:42:16 +00001010 }
1011
1012 pair<iterator, bool> __node_insert_unique(__node_pointer __nd);
1013 iterator __node_insert_multi(__node_pointer __nd);
1014 iterator __node_insert_multi(const_iterator __p,
1015 __node_pointer __nd);
1016
Eric Fiselierfcd02212016-02-11 11:59:44 +00001017#ifndef _LIBCPP_CXX03_LANG
1018 template <class _Key, class ..._Args>
Duncan P. N. Exon Smithfde79b42016-03-17 20:45:20 +00001019 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierfcd02212016-02-11 11:59:44 +00001020 pair<iterator, bool> __emplace_unique_key_args(_Key const& __k, _Args&&... __args);
Howard Hinnant3e519522010-05-11 19:42:16 +00001021
Eric Fiselierfcd02212016-02-11 11:59:44 +00001022 template <class... _Args>
Duncan P. N. Exon Smithfde79b42016-03-17 20:45:20 +00001023 _LIBCPP_INLINE_VISIBILITY
1024 pair<iterator, bool> __emplace_unique_impl(_Args&&... __args);
1025
1026 template <class _Pp>
1027 _LIBCPP_INLINE_VISIBILITY
1028 pair<iterator, bool> __emplace_unique(_Pp&& __x) {
1029 return __emplace_unique_extract_key(_VSTD::forward<_Pp>(__x),
1030 __can_extract_key<_Pp, key_type>());
1031 }
Eric Fiselier50088682016-04-16 00:23:12 +00001032
1033 template <class _First, class _Second>
1034 _LIBCPP_INLINE_VISIBILITY
1035 typename enable_if<
1036 __can_extract_map_key<_First, key_type, __container_value_type>::value,
1037 pair<iterator, bool>
1038 >::type __emplace_unique(_First&& __f, _Second&& __s) {
1039 return __emplace_unique_key_args(__f, _VSTD::forward<_First>(__f),
1040 _VSTD::forward<_Second>(__s));
1041 }
1042
Eric Fiselierfcd02212016-02-11 11:59:44 +00001043 template <class... _Args>
Duncan P. N. Exon Smithfde79b42016-03-17 20:45:20 +00001044 _LIBCPP_INLINE_VISIBILITY
1045 pair<iterator, bool> __emplace_unique(_Args&&... __args) {
1046 return __emplace_unique_impl(_VSTD::forward<_Args>(__args)...);
1047 }
1048
1049 template <class _Pp>
1050 _LIBCPP_INLINE_VISIBILITY
1051 pair<iterator, bool>
1052 __emplace_unique_extract_key(_Pp&& __x, __extract_key_fail_tag) {
1053 return __emplace_unique_impl(_VSTD::forward<_Pp>(__x));
1054 }
1055 template <class _Pp>
1056 _LIBCPP_INLINE_VISIBILITY
1057 pair<iterator, bool>
1058 __emplace_unique_extract_key(_Pp&& __x, __extract_key_self_tag) {
1059 return __emplace_unique_key_args(__x, _VSTD::forward<_Pp>(__x));
1060 }
1061 template <class _Pp>
1062 _LIBCPP_INLINE_VISIBILITY
1063 pair<iterator, bool>
1064 __emplace_unique_extract_key(_Pp&& __x, __extract_key_first_tag) {
1065 return __emplace_unique_key_args(__x.first, _VSTD::forward<_Pp>(__x));
1066 }
1067
1068 template <class... _Args>
1069 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierfcd02212016-02-11 11:59:44 +00001070 iterator __emplace_multi(_Args&&... __args);
1071 template <class... _Args>
Duncan P. N. Exon Smithfde79b42016-03-17 20:45:20 +00001072 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierfcd02212016-02-11 11:59:44 +00001073 iterator __emplace_hint_multi(const_iterator __p, _Args&&... __args);
1074
1075
Eric Fiselierde3f2b32015-06-13 07:18:32 +00001076 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierfcd02212016-02-11 11:59:44 +00001077 pair<iterator, bool>
1078 __insert_unique(__container_value_type&& __x) {
1079 return __emplace_unique_key_args(_NodeTypes::__get_key(__x), _VSTD::move(__x));
1080 }
1081
1082 template <class _Pp, class = typename enable_if<
1083 !__is_same_uncvref<_Pp, __container_value_type>::value
1084 >::type>
Eric Fiselierde3f2b32015-06-13 07:18:32 +00001085 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierfcd02212016-02-11 11:59:44 +00001086 pair<iterator, bool> __insert_unique(_Pp&& __x) {
1087 return __emplace_unique(_VSTD::forward<_Pp>(__x));
1088 }
1089
1090 template <class _Pp>
1091 _LIBCPP_INLINE_VISIBILITY
1092 iterator __insert_multi(_Pp&& __x) {
1093 return __emplace_multi(_VSTD::forward<_Pp>(__x));
1094 }
1095
1096 template <class _Pp>
1097 _LIBCPP_INLINE_VISIBILITY
1098 iterator __insert_multi(const_iterator __p, _Pp&& __x) {
1099 return __emplace_hint_multi(__p, _VSTD::forward<_Pp>(__x));
1100 }
1101
1102#else // !defined(_LIBCPP_CXX03_LANG)
1103 template <class _Key, class _Args>
Eric Fiselier4271d012016-09-25 04:05:46 +00001104 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierfcd02212016-02-11 11:59:44 +00001105 pair<iterator, bool> __emplace_unique_key_args(_Key const&, _Args& __args);
1106
1107 iterator __insert_multi(const __container_value_type& __x);
1108 iterator __insert_multi(const_iterator __p, const __container_value_type& __x);
Eric Fiselierde3f2b32015-06-13 07:18:32 +00001109#endif
1110
Eric Fiselierfcd02212016-02-11 11:59:44 +00001111 _LIBCPP_INLINE_VISIBILITY
1112 pair<iterator, bool> __insert_unique(const __container_value_type& __x) {
1113 return __emplace_unique_key_args(_NodeTypes::__get_key(__x), __x);
1114 }
Howard Hinnant3e519522010-05-11 19:42:16 +00001115
Howard Hinnant37141072011-06-04 18:54:24 +00001116 void clear() _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +00001117 void rehash(size_type __n);
Howard Hinnant43d99232010-09-21 17:32:39 +00001118 _LIBCPP_INLINE_VISIBILITY void reserve(size_type __n)
Howard Hinnant3e519522010-05-11 19:42:16 +00001119 {rehash(static_cast<size_type>(ceil(__n / max_load_factor())));}
Howard Hinnant43d99232010-09-21 17:32:39 +00001120
1121 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001122 size_type bucket_count() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001123 {
1124 return __bucket_list_.get_deleter().size();
1125 }
1126
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001127 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001128 iterator begin() _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001129 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001130 iterator end() _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001131 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001132 const_iterator begin() const _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001133 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001134 const_iterator end() const _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +00001135
1136 template <class _Key>
Howard Hinnant43d99232010-09-21 17:32:39 +00001137 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001138 size_type bucket(const _Key& __k) const
Howard Hinnante5c13de2013-07-29 19:05:47 +00001139 {
1140 _LIBCPP_ASSERT(bucket_count() > 0,
1141 "unordered container::bucket(key) called when bucket_count() == 0");
1142 return __constrain_hash(hash_function()(__k), bucket_count());
1143 }
Howard Hinnant3e519522010-05-11 19:42:16 +00001144
1145 template <class _Key>
1146 iterator find(const _Key& __x);
1147 template <class _Key>
1148 const_iterator find(const _Key& __x) const;
1149
Howard Hinnantc003db12011-11-29 18:15:50 +00001150 typedef __hash_node_destructor<__node_allocator> _Dp;
1151 typedef unique_ptr<__node, _Dp> __node_holder;
Howard Hinnant3e519522010-05-11 19:42:16 +00001152
1153 iterator erase(const_iterator __p);
1154 iterator erase(const_iterator __first, const_iterator __last);
1155 template <class _Key>
1156 size_type __erase_unique(const _Key& __k);
1157 template <class _Key>
1158 size_type __erase_multi(const _Key& __k);
Howard Hinnant37141072011-06-04 18:54:24 +00001159 __node_holder remove(const_iterator __p) _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +00001160
1161 template <class _Key>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001162 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001163 size_type __count_unique(const _Key& __k) const;
1164 template <class _Key>
1165 size_type __count_multi(const _Key& __k) const;
1166
1167 template <class _Key>
1168 pair<iterator, iterator>
1169 __equal_range_unique(const _Key& __k);
1170 template <class _Key>
1171 pair<const_iterator, const_iterator>
1172 __equal_range_unique(const _Key& __k) const;
1173
1174 template <class _Key>
1175 pair<iterator, iterator>
1176 __equal_range_multi(const _Key& __k);
1177 template <class _Key>
1178 pair<const_iterator, const_iterator>
1179 __equal_range_multi(const _Key& __k) const;
1180
Howard Hinnant37141072011-06-04 18:54:24 +00001181 void swap(__hash_table& __u)
Eric Fiselier87a82492015-07-18 20:40:46 +00001182#if _LIBCPP_STD_VER <= 11
Eric Fiselier780b51d2016-12-28 05:53:01 +00001183 _NOEXCEPT_DEBUG_(
Marshall Clowe3fbe142015-07-13 20:04:56 +00001184 __is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value
Marshall Clowe3fbe142015-07-13 20:04:56 +00001185 && (!allocator_traits<__pointer_allocator>::propagate_on_container_swap::value
1186 || __is_nothrow_swappable<__pointer_allocator>::value)
1187 && (!__node_traits::propagate_on_container_swap::value
1188 || __is_nothrow_swappable<__node_allocator>::value)
Marshall Clowe3fbe142015-07-13 20:04:56 +00001189 );
Eric Fiselier87a82492015-07-18 20:40:46 +00001190#else
Eric Fiselier780b51d2016-12-28 05:53:01 +00001191 _NOEXCEPT_DEBUG_(__is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value);
Eric Fiselier87a82492015-07-18 20:40:46 +00001192#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001193
Howard Hinnant43d99232010-09-21 17:32:39 +00001194 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001195 size_type max_bucket_count() const _NOEXCEPT
Eric Fiselier55b31b4e2016-11-23 01:18:56 +00001196 {return max_size(); }
Howard Hinnant3e519522010-05-11 19:42:16 +00001197 size_type bucket_size(size_type __n) const;
Howard Hinnant37141072011-06-04 18:54:24 +00001198 _LIBCPP_INLINE_VISIBILITY float load_factor() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001199 {
1200 size_type __bc = bucket_count();
1201 return __bc != 0 ? (float)size() / __bc : 0.f;
1202 }
Howard Hinnant37141072011-06-04 18:54:24 +00001203 _LIBCPP_INLINE_VISIBILITY void max_load_factor(float __mlf) _NOEXCEPT
Howard Hinnante5c13de2013-07-29 19:05:47 +00001204 {
1205 _LIBCPP_ASSERT(__mlf > 0,
1206 "unordered container::max_load_factor(lf) called with lf <= 0");
1207 max_load_factor() = _VSTD::max(__mlf, load_factor());
1208 }
Howard Hinnant3e519522010-05-11 19:42:16 +00001209
Howard Hinnantb24c8022013-07-23 22:01:58 +00001210 _LIBCPP_INLINE_VISIBILITY
1211 local_iterator
1212 begin(size_type __n)
1213 {
Howard Hinnante5c13de2013-07-29 19:05:47 +00001214 _LIBCPP_ASSERT(__n < bucket_count(),
1215 "unordered container::begin(n) called with n >= bucket_count()");
Howard Hinnantb24c8022013-07-23 22:01:58 +00001216#if _LIBCPP_DEBUG_LEVEL >= 2
1217 return local_iterator(__bucket_list_[__n], __n, bucket_count(), this);
1218#else
1219 return local_iterator(__bucket_list_[__n], __n, bucket_count());
1220#endif
1221 }
1222
1223 _LIBCPP_INLINE_VISIBILITY
1224 local_iterator
1225 end(size_type __n)
1226 {
Howard Hinnante5c13de2013-07-29 19:05:47 +00001227 _LIBCPP_ASSERT(__n < bucket_count(),
1228 "unordered container::end(n) called with n >= bucket_count()");
Howard Hinnantb24c8022013-07-23 22:01:58 +00001229#if _LIBCPP_DEBUG_LEVEL >= 2
1230 return local_iterator(nullptr, __n, bucket_count(), this);
1231#else
1232 return local_iterator(nullptr, __n, bucket_count());
1233#endif
1234 }
1235
1236 _LIBCPP_INLINE_VISIBILITY
1237 const_local_iterator
1238 cbegin(size_type __n) const
1239 {
Howard Hinnante5c13de2013-07-29 19:05:47 +00001240 _LIBCPP_ASSERT(__n < bucket_count(),
1241 "unordered container::cbegin(n) called with n >= bucket_count()");
Howard Hinnantb24c8022013-07-23 22:01:58 +00001242#if _LIBCPP_DEBUG_LEVEL >= 2
1243 return const_local_iterator(__bucket_list_[__n], __n, bucket_count(), this);
1244#else
1245 return const_local_iterator(__bucket_list_[__n], __n, bucket_count());
1246#endif
1247 }
1248
1249 _LIBCPP_INLINE_VISIBILITY
1250 const_local_iterator
1251 cend(size_type __n) const
1252 {
Howard Hinnante5c13de2013-07-29 19:05:47 +00001253 _LIBCPP_ASSERT(__n < bucket_count(),
1254 "unordered container::cend(n) called with n >= bucket_count()");
Howard Hinnantb24c8022013-07-23 22:01:58 +00001255#if _LIBCPP_DEBUG_LEVEL >= 2
1256 return const_local_iterator(nullptr, __n, bucket_count(), this);
1257#else
1258 return const_local_iterator(nullptr, __n, bucket_count());
1259#endif
1260 }
1261
1262#if _LIBCPP_DEBUG_LEVEL >= 2
1263
1264 bool __dereferenceable(const const_iterator* __i) const;
1265 bool __decrementable(const const_iterator* __i) const;
1266 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1267 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1268
1269#endif // _LIBCPP_DEBUG_LEVEL >= 2
1270
Howard Hinnant3e519522010-05-11 19:42:16 +00001271private:
1272 void __rehash(size_type __n);
1273
Eric Fiselierfcd02212016-02-11 11:59:44 +00001274#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001275 template <class ..._Args>
Eric Fiselierfcd02212016-02-11 11:59:44 +00001276 __node_holder __construct_node(_Args&& ...__args);
1277
1278 template <class _First, class ..._Rest>
1279 __node_holder __construct_node_hash(size_t __hash, _First&& __f, _Rest&&... __rest);
1280#else // _LIBCPP_CXX03_LANG
1281 __node_holder __construct_node(const __container_value_type& __v);
1282 __node_holder __construct_node_hash(size_t __hash, const __container_value_type& __v);
Howard Hinnant3e519522010-05-11 19:42:16 +00001283#endif
Eric Fiselierfcd02212016-02-11 11:59:44 +00001284
Howard Hinnant3e519522010-05-11 19:42:16 +00001285
Howard Hinnant43d99232010-09-21 17:32:39 +00001286 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001287 void __copy_assign_alloc(const __hash_table& __u)
1288 {__copy_assign_alloc(__u, integral_constant<bool,
1289 __node_traits::propagate_on_container_copy_assignment::value>());}
1290 void __copy_assign_alloc(const __hash_table& __u, true_type);
Howard Hinnant43d99232010-09-21 17:32:39 +00001291 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2063662011-12-01 20:21:04 +00001292 void __copy_assign_alloc(const __hash_table&, false_type) {}
Howard Hinnant3e519522010-05-11 19:42:16 +00001293
Eric Fiselierfcd02212016-02-11 11:59:44 +00001294#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001295 void __move_assign(__hash_table& __u, false_type);
Howard Hinnant37141072011-06-04 18:54:24 +00001296 void __move_assign(__hash_table& __u, true_type)
1297 _NOEXCEPT_(
1298 is_nothrow_move_assignable<__node_allocator>::value &&
1299 is_nothrow_move_assignable<hasher>::value &&
1300 is_nothrow_move_assignable<key_equal>::value);
1301 _LIBCPP_INLINE_VISIBILITY
1302 void __move_assign_alloc(__hash_table& __u)
1303 _NOEXCEPT_(
1304 !__node_traits::propagate_on_container_move_assignment::value ||
1305 (is_nothrow_move_assignable<__pointer_allocator>::value &&
1306 is_nothrow_move_assignable<__node_allocator>::value))
Howard Hinnant3e519522010-05-11 19:42:16 +00001307 {__move_assign_alloc(__u, integral_constant<bool,
1308 __node_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnant43d99232010-09-21 17:32:39 +00001309 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001310 void __move_assign_alloc(__hash_table& __u, true_type)
Howard Hinnant37141072011-06-04 18:54:24 +00001311 _NOEXCEPT_(
1312 is_nothrow_move_assignable<__pointer_allocator>::value &&
1313 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +00001314 {
1315 __bucket_list_.get_deleter().__alloc() =
Howard Hinnantce48a112011-06-30 21:18:19 +00001316 _VSTD::move(__u.__bucket_list_.get_deleter().__alloc());
1317 __node_alloc() = _VSTD::move(__u.__node_alloc());
Howard Hinnant3e519522010-05-11 19:42:16 +00001318 }
Howard Hinnant43d99232010-09-21 17:32:39 +00001319 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001320 void __move_assign_alloc(__hash_table&, false_type) _NOEXCEPT {}
Eric Fiselierfcd02212016-02-11 11:59:44 +00001321#endif // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001322
Eric Fiseliercd71f442017-01-07 03:01:24 +00001323 void __deallocate_node(__next_pointer __np) _NOEXCEPT;
Eric Fiselier40492ba2016-07-23 20:36:55 +00001324 __next_pointer __detach() _NOEXCEPT;
Howard Hinnant307f8142013-06-22 15:21:29 +00001325
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +00001326 template <class, class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS unordered_map;
1327 template <class, class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS unordered_multimap;
Howard Hinnant3e519522010-05-11 19:42:16 +00001328};
1329
1330template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001331inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001332__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table()
Howard Hinnant37141072011-06-04 18:54:24 +00001333 _NOEXCEPT_(
1334 is_nothrow_default_constructible<__bucket_list>::value &&
1335 is_nothrow_default_constructible<__first_node>::value &&
Eric Fiseliere2e332a2015-12-16 00:53:04 +00001336 is_nothrow_default_constructible<__node_allocator>::value &&
Howard Hinnant37141072011-06-04 18:54:24 +00001337 is_nothrow_default_constructible<hasher>::value &&
1338 is_nothrow_default_constructible<key_equal>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +00001339 : __p2_(0),
1340 __p3_(1.0f)
1341{
1342}
1343
1344template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001345inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001346__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const hasher& __hf,
1347 const key_equal& __eql)
1348 : __bucket_list_(nullptr, __bucket_list_deleter()),
1349 __p1_(),
1350 __p2_(0, __hf),
1351 __p3_(1.0f, __eql)
1352{
1353}
1354
1355template <class _Tp, class _Hash, class _Equal, class _Alloc>
1356__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const hasher& __hf,
1357 const key_equal& __eql,
1358 const allocator_type& __a)
1359 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1360 __p1_(__node_allocator(__a)),
1361 __p2_(0, __hf),
1362 __p3_(1.0f, __eql)
1363{
1364}
1365
1366template <class _Tp, class _Hash, class _Equal, class _Alloc>
1367__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const allocator_type& __a)
1368 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1369 __p1_(__node_allocator(__a)),
1370 __p2_(0),
1371 __p3_(1.0f)
1372{
1373}
1374
1375template <class _Tp, class _Hash, class _Equal, class _Alloc>
1376__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const __hash_table& __u)
1377 : __bucket_list_(nullptr,
1378 __bucket_list_deleter(allocator_traits<__pointer_allocator>::
1379 select_on_container_copy_construction(
1380 __u.__bucket_list_.get_deleter().__alloc()), 0)),
1381 __p1_(allocator_traits<__node_allocator>::
1382 select_on_container_copy_construction(__u.__node_alloc())),
1383 __p2_(0, __u.hash_function()),
1384 __p3_(__u.__p3_)
1385{
1386}
1387
1388template <class _Tp, class _Hash, class _Equal, class _Alloc>
1389__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const __hash_table& __u,
1390 const allocator_type& __a)
1391 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1392 __p1_(__node_allocator(__a)),
1393 __p2_(0, __u.hash_function()),
1394 __p3_(__u.__p3_)
1395{
1396}
1397
Eric Fiselierfcd02212016-02-11 11:59:44 +00001398#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001399
1400template <class _Tp, class _Hash, class _Equal, class _Alloc>
1401__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(__hash_table&& __u)
Howard Hinnant37141072011-06-04 18:54:24 +00001402 _NOEXCEPT_(
1403 is_nothrow_move_constructible<__bucket_list>::value &&
1404 is_nothrow_move_constructible<__first_node>::value &&
Eric Fiseliere2e332a2015-12-16 00:53:04 +00001405 is_nothrow_move_constructible<__node_allocator>::value &&
Howard Hinnant37141072011-06-04 18:54:24 +00001406 is_nothrow_move_constructible<hasher>::value &&
1407 is_nothrow_move_constructible<key_equal>::value)
Howard Hinnantce48a112011-06-30 21:18:19 +00001408 : __bucket_list_(_VSTD::move(__u.__bucket_list_)),
1409 __p1_(_VSTD::move(__u.__p1_)),
1410 __p2_(_VSTD::move(__u.__p2_)),
1411 __p3_(_VSTD::move(__u.__p3_))
Howard Hinnant3e519522010-05-11 19:42:16 +00001412{
1413 if (size() > 0)
1414 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001415 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash(), bucket_count())] =
1416 __p1_.first().__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001417 __u.__p1_.first().__next_ = nullptr;
1418 __u.size() = 0;
1419 }
1420}
1421
1422template <class _Tp, class _Hash, class _Equal, class _Alloc>
1423__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(__hash_table&& __u,
1424 const allocator_type& __a)
1425 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1426 __p1_(__node_allocator(__a)),
Howard Hinnantce48a112011-06-30 21:18:19 +00001427 __p2_(0, _VSTD::move(__u.hash_function())),
1428 __p3_(_VSTD::move(__u.__p3_))
Howard Hinnant3e519522010-05-11 19:42:16 +00001429{
1430 if (__a == allocator_type(__u.__node_alloc()))
1431 {
1432 __bucket_list_.reset(__u.__bucket_list_.release());
1433 __bucket_list_.get_deleter().size() = __u.__bucket_list_.get_deleter().size();
1434 __u.__bucket_list_.get_deleter().size() = 0;
1435 if (__u.size() > 0)
1436 {
1437 __p1_.first().__next_ = __u.__p1_.first().__next_;
1438 __u.__p1_.first().__next_ = nullptr;
Eric Fiselier40492ba2016-07-23 20:36:55 +00001439 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash(), bucket_count())] =
1440 __p1_.first().__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001441 size() = __u.size();
1442 __u.size() = 0;
1443 }
1444 }
1445}
1446
Eric Fiselierfcd02212016-02-11 11:59:44 +00001447#endif // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001448
1449template <class _Tp, class _Hash, class _Equal, class _Alloc>
1450__hash_table<_Tp, _Hash, _Equal, _Alloc>::~__hash_table()
1451{
Marshall Clow3b8669e2016-06-30 22:05:45 +00001452 static_assert((is_copy_constructible<key_equal>::value),
1453 "Predicate must be copy-constructible.");
1454 static_assert((is_copy_constructible<hasher>::value),
1455 "Hasher must be copy-constructible.");
Eric Fiseliercd71f442017-01-07 03:01:24 +00001456 __deallocate_node(__p1_.first().__next_);
Howard Hinnantb24c8022013-07-23 22:01:58 +00001457#if _LIBCPP_DEBUG_LEVEL >= 2
1458 __get_db()->__erase_c(this);
1459#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001460}
1461
1462template <class _Tp, class _Hash, class _Equal, class _Alloc>
1463void
1464__hash_table<_Tp, _Hash, _Equal, _Alloc>::__copy_assign_alloc(
1465 const __hash_table& __u, true_type)
1466{
1467 if (__node_alloc() != __u.__node_alloc())
1468 {
1469 clear();
1470 __bucket_list_.reset();
1471 __bucket_list_.get_deleter().size() = 0;
1472 }
1473 __bucket_list_.get_deleter().__alloc() = __u.__bucket_list_.get_deleter().__alloc();
1474 __node_alloc() = __u.__node_alloc();
1475}
1476
1477template <class _Tp, class _Hash, class _Equal, class _Alloc>
1478__hash_table<_Tp, _Hash, _Equal, _Alloc>&
1479__hash_table<_Tp, _Hash, _Equal, _Alloc>::operator=(const __hash_table& __u)
1480{
1481 if (this != &__u)
1482 {
1483 __copy_assign_alloc(__u);
1484 hash_function() = __u.hash_function();
1485 key_eq() = __u.key_eq();
1486 max_load_factor() = __u.max_load_factor();
1487 __assign_multi(__u.begin(), __u.end());
1488 }
1489 return *this;
1490}
1491
1492template <class _Tp, class _Hash, class _Equal, class _Alloc>
1493void
Eric Fiseliercd71f442017-01-07 03:01:24 +00001494__hash_table<_Tp, _Hash, _Equal, _Alloc>::__deallocate_node(__next_pointer __np)
Howard Hinnant37141072011-06-04 18:54:24 +00001495 _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001496{
1497 __node_allocator& __na = __node_alloc();
1498 while (__np != nullptr)
1499 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001500 __next_pointer __next = __np->__next_;
Howard Hinnantb24c8022013-07-23 22:01:58 +00001501#if _LIBCPP_DEBUG_LEVEL >= 2
1502 __c_node* __c = __get_db()->__find_c_and_lock(this);
1503 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1504 {
1505 --__p;
1506 iterator* __i = static_cast<iterator*>((*__p)->__i_);
1507 if (__i->__node_ == __np)
1508 {
1509 (*__p)->__c_ = nullptr;
1510 if (--__c->end_ != __p)
1511 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1512 }
1513 }
1514 __get_db()->unlock();
1515#endif
Eric Fiselier40492ba2016-07-23 20:36:55 +00001516 __node_pointer __real_np = __np->__upcast();
1517 __node_traits::destroy(__na, _NodeTypes::__get_ptr(__real_np->__value_));
1518 __node_traits::deallocate(__na, __real_np, 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001519 __np = __next;
1520 }
1521}
1522
1523template <class _Tp, class _Hash, class _Equal, class _Alloc>
Eric Fiselier40492ba2016-07-23 20:36:55 +00001524typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__next_pointer
Howard Hinnant37141072011-06-04 18:54:24 +00001525__hash_table<_Tp, _Hash, _Equal, _Alloc>::__detach() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001526{
1527 size_type __bc = bucket_count();
1528 for (size_type __i = 0; __i < __bc; ++__i)
1529 __bucket_list_[__i] = nullptr;
1530 size() = 0;
Eric Fiselier40492ba2016-07-23 20:36:55 +00001531 __next_pointer __cache = __p1_.first().__next_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001532 __p1_.first().__next_ = nullptr;
1533 return __cache;
1534}
1535
Eric Fiselierfcd02212016-02-11 11:59:44 +00001536#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001537
1538template <class _Tp, class _Hash, class _Equal, class _Alloc>
1539void
1540__hash_table<_Tp, _Hash, _Equal, _Alloc>::__move_assign(
1541 __hash_table& __u, true_type)
Howard Hinnant37141072011-06-04 18:54:24 +00001542 _NOEXCEPT_(
1543 is_nothrow_move_assignable<__node_allocator>::value &&
1544 is_nothrow_move_assignable<hasher>::value &&
1545 is_nothrow_move_assignable<key_equal>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +00001546{
1547 clear();
1548 __bucket_list_.reset(__u.__bucket_list_.release());
1549 __bucket_list_.get_deleter().size() = __u.__bucket_list_.get_deleter().size();
1550 __u.__bucket_list_.get_deleter().size() = 0;
1551 __move_assign_alloc(__u);
1552 size() = __u.size();
Howard Hinnantce48a112011-06-30 21:18:19 +00001553 hash_function() = _VSTD::move(__u.hash_function());
Howard Hinnant3e519522010-05-11 19:42:16 +00001554 max_load_factor() = __u.max_load_factor();
Howard Hinnantce48a112011-06-30 21:18:19 +00001555 key_eq() = _VSTD::move(__u.key_eq());
Howard Hinnant3e519522010-05-11 19:42:16 +00001556 __p1_.first().__next_ = __u.__p1_.first().__next_;
1557 if (size() > 0)
1558 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001559 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash(), bucket_count())] =
1560 __p1_.first().__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001561 __u.__p1_.first().__next_ = nullptr;
1562 __u.size() = 0;
1563 }
Howard Hinnantb24c8022013-07-23 22:01:58 +00001564#if _LIBCPP_DEBUG_LEVEL >= 2
1565 __get_db()->swap(this, &__u);
1566#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001567}
1568
1569template <class _Tp, class _Hash, class _Equal, class _Alloc>
1570void
1571__hash_table<_Tp, _Hash, _Equal, _Alloc>::__move_assign(
1572 __hash_table& __u, false_type)
1573{
1574 if (__node_alloc() == __u.__node_alloc())
1575 __move_assign(__u, true_type());
1576 else
1577 {
Howard Hinnantce48a112011-06-30 21:18:19 +00001578 hash_function() = _VSTD::move(__u.hash_function());
1579 key_eq() = _VSTD::move(__u.key_eq());
Howard Hinnant3e519522010-05-11 19:42:16 +00001580 max_load_factor() = __u.max_load_factor();
1581 if (bucket_count() != 0)
1582 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001583 __next_pointer __cache = __detach();
Howard Hinnant3e519522010-05-11 19:42:16 +00001584#ifndef _LIBCPP_NO_EXCEPTIONS
1585 try
1586 {
Howard Hinnantb3371f62010-08-22 00:02:43 +00001587#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001588 const_iterator __i = __u.begin();
1589 while (__cache != nullptr && __u.size() != 0)
1590 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001591 __cache->__upcast()->__value_ =
1592 _VSTD::move(__u.remove(__i++)->__value_);
1593 __next_pointer __next = __cache->__next_;
1594 __node_insert_multi(__cache->__upcast());
Howard Hinnant3e519522010-05-11 19:42:16 +00001595 __cache = __next;
1596 }
1597#ifndef _LIBCPP_NO_EXCEPTIONS
1598 }
1599 catch (...)
1600 {
Eric Fiseliercd71f442017-01-07 03:01:24 +00001601 __deallocate_node(__cache);
Howard Hinnant3e519522010-05-11 19:42:16 +00001602 throw;
1603 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00001604#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiseliercd71f442017-01-07 03:01:24 +00001605 __deallocate_node(__cache);
Howard Hinnant3e519522010-05-11 19:42:16 +00001606 }
1607 const_iterator __i = __u.begin();
1608 while (__u.size() != 0)
1609 {
Eric Fiselierfcd02212016-02-11 11:59:44 +00001610 __node_holder __h = __construct_node(_NodeTypes::__move(__u.remove(__i++)->__value_));
Howard Hinnant3e519522010-05-11 19:42:16 +00001611 __node_insert_multi(__h.get());
1612 __h.release();
1613 }
1614 }
1615}
1616
1617template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001618inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001619__hash_table<_Tp, _Hash, _Equal, _Alloc>&
1620__hash_table<_Tp, _Hash, _Equal, _Alloc>::operator=(__hash_table&& __u)
Howard Hinnant37141072011-06-04 18:54:24 +00001621 _NOEXCEPT_(
1622 __node_traits::propagate_on_container_move_assignment::value &&
1623 is_nothrow_move_assignable<__node_allocator>::value &&
1624 is_nothrow_move_assignable<hasher>::value &&
1625 is_nothrow_move_assignable<key_equal>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +00001626{
1627 __move_assign(__u, integral_constant<bool,
1628 __node_traits::propagate_on_container_move_assignment::value>());
1629 return *this;
1630}
1631
Eric Fiselierfcd02212016-02-11 11:59:44 +00001632#endif // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001633
1634template <class _Tp, class _Hash, class _Equal, class _Alloc>
1635template <class _InputIterator>
1636void
1637__hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_unique(_InputIterator __first,
1638 _InputIterator __last)
1639{
Eric Fiselierfcd02212016-02-11 11:59:44 +00001640 typedef iterator_traits<_InputIterator> _ITraits;
1641 typedef typename _ITraits::value_type _ItValueType;
1642 static_assert((is_same<_ItValueType, __container_value_type>::value),
1643 "__assign_unique may only be called with the containers value type");
1644
Howard Hinnant3e519522010-05-11 19:42:16 +00001645 if (bucket_count() != 0)
1646 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001647 __next_pointer __cache = __detach();
Howard Hinnant3e519522010-05-11 19:42:16 +00001648#ifndef _LIBCPP_NO_EXCEPTIONS
1649 try
1650 {
Howard Hinnantb3371f62010-08-22 00:02:43 +00001651#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001652 for (; __cache != nullptr && __first != __last; ++__first)
1653 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001654 __cache->__upcast()->__value_ = *__first;
1655 __next_pointer __next = __cache->__next_;
1656 __node_insert_unique(__cache->__upcast());
Howard Hinnant3e519522010-05-11 19:42:16 +00001657 __cache = __next;
1658 }
1659#ifndef _LIBCPP_NO_EXCEPTIONS
1660 }
1661 catch (...)
1662 {
Eric Fiseliercd71f442017-01-07 03:01:24 +00001663 __deallocate_node(__cache);
Howard Hinnant3e519522010-05-11 19:42:16 +00001664 throw;
1665 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00001666#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiseliercd71f442017-01-07 03:01:24 +00001667 __deallocate_node(__cache);
Howard Hinnant3e519522010-05-11 19:42:16 +00001668 }
1669 for (; __first != __last; ++__first)
1670 __insert_unique(*__first);
1671}
1672
1673template <class _Tp, class _Hash, class _Equal, class _Alloc>
1674template <class _InputIterator>
1675void
1676__hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_multi(_InputIterator __first,
1677 _InputIterator __last)
1678{
Eric Fiselierfcd02212016-02-11 11:59:44 +00001679 typedef iterator_traits<_InputIterator> _ITraits;
1680 typedef typename _ITraits::value_type _ItValueType;
1681 static_assert((is_same<_ItValueType, __container_value_type>::value ||
1682 is_same<_ItValueType, __node_value_type>::value),
1683 "__assign_multi may only be called with the containers value type"
1684 " or the nodes value type");
Howard Hinnant3e519522010-05-11 19:42:16 +00001685 if (bucket_count() != 0)
1686 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001687 __next_pointer __cache = __detach();
Howard Hinnant3e519522010-05-11 19:42:16 +00001688#ifndef _LIBCPP_NO_EXCEPTIONS
1689 try
1690 {
Howard Hinnantb3371f62010-08-22 00:02:43 +00001691#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001692 for (; __cache != nullptr && __first != __last; ++__first)
1693 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001694 __cache->__upcast()->__value_ = *__first;
1695 __next_pointer __next = __cache->__next_;
1696 __node_insert_multi(__cache->__upcast());
Howard Hinnant3e519522010-05-11 19:42:16 +00001697 __cache = __next;
1698 }
1699#ifndef _LIBCPP_NO_EXCEPTIONS
1700 }
1701 catch (...)
1702 {
Eric Fiseliercd71f442017-01-07 03:01:24 +00001703 __deallocate_node(__cache);
Howard Hinnant3e519522010-05-11 19:42:16 +00001704 throw;
1705 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00001706#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiseliercd71f442017-01-07 03:01:24 +00001707 __deallocate_node(__cache);
Howard Hinnant3e519522010-05-11 19:42:16 +00001708 }
1709 for (; __first != __last; ++__first)
Eric Fiselierfcd02212016-02-11 11:59:44 +00001710 __insert_multi(_NodeTypes::__get_value(*__first));
Howard Hinnant3e519522010-05-11 19:42:16 +00001711}
1712
1713template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001714inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001715typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
Howard Hinnant37141072011-06-04 18:54:24 +00001716__hash_table<_Tp, _Hash, _Equal, _Alloc>::begin() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001717{
Howard Hinnantb24c8022013-07-23 22:01:58 +00001718#if _LIBCPP_DEBUG_LEVEL >= 2
1719 return iterator(__p1_.first().__next_, this);
1720#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001721 return iterator(__p1_.first().__next_);
Howard Hinnantb24c8022013-07-23 22:01:58 +00001722#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001723}
1724
1725template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001726inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001727typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
Howard Hinnant37141072011-06-04 18:54:24 +00001728__hash_table<_Tp, _Hash, _Equal, _Alloc>::end() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001729{
Howard Hinnantb24c8022013-07-23 22:01:58 +00001730#if _LIBCPP_DEBUG_LEVEL >= 2
1731 return iterator(nullptr, this);
1732#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001733 return iterator(nullptr);
Howard Hinnantb24c8022013-07-23 22:01:58 +00001734#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001735}
1736
1737template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001738inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001739typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
Howard Hinnant37141072011-06-04 18:54:24 +00001740__hash_table<_Tp, _Hash, _Equal, _Alloc>::begin() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001741{
Howard Hinnantb24c8022013-07-23 22:01:58 +00001742#if _LIBCPP_DEBUG_LEVEL >= 2
1743 return const_iterator(__p1_.first().__next_, this);
1744#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001745 return const_iterator(__p1_.first().__next_);
Howard Hinnantb24c8022013-07-23 22:01:58 +00001746#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001747}
1748
1749template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001750inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001751typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
Howard Hinnant37141072011-06-04 18:54:24 +00001752__hash_table<_Tp, _Hash, _Equal, _Alloc>::end() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001753{
Howard Hinnantb24c8022013-07-23 22:01:58 +00001754#if _LIBCPP_DEBUG_LEVEL >= 2
1755 return const_iterator(nullptr, this);
1756#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001757 return const_iterator(nullptr);
Howard Hinnantb24c8022013-07-23 22:01:58 +00001758#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001759}
1760
1761template <class _Tp, class _Hash, class _Equal, class _Alloc>
1762void
Howard Hinnant37141072011-06-04 18:54:24 +00001763__hash_table<_Tp, _Hash, _Equal, _Alloc>::clear() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001764{
1765 if (size() > 0)
1766 {
Eric Fiseliercd71f442017-01-07 03:01:24 +00001767 __deallocate_node(__p1_.first().__next_);
Howard Hinnant3e519522010-05-11 19:42:16 +00001768 __p1_.first().__next_ = nullptr;
1769 size_type __bc = bucket_count();
Howard Hinnant1f8da842011-07-05 14:14:17 +00001770 for (size_type __i = 0; __i < __bc; ++__i)
Howard Hinnant3e519522010-05-11 19:42:16 +00001771 __bucket_list_[__i] = nullptr;
1772 size() = 0;
1773 }
1774}
1775
1776template <class _Tp, class _Hash, class _Equal, class _Alloc>
1777pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
1778__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_unique(__node_pointer __nd)
1779{
1780 __nd->__hash_ = hash_function()(__nd->__value_);
1781 size_type __bc = bucket_count();
1782 bool __inserted = false;
Eric Fiselier40492ba2016-07-23 20:36:55 +00001783 __next_pointer __ndptr;
Howard Hinnant3e519522010-05-11 19:42:16 +00001784 size_t __chash;
1785 if (__bc != 0)
1786 {
Howard Hinnant4cb38a82012-07-06 17:31:14 +00001787 __chash = __constrain_hash(__nd->__hash_, __bc);
Howard Hinnant3e519522010-05-11 19:42:16 +00001788 __ndptr = __bucket_list_[__chash];
1789 if (__ndptr != nullptr)
1790 {
1791 for (__ndptr = __ndptr->__next_; __ndptr != nullptr &&
Eric Fiselier40492ba2016-07-23 20:36:55 +00001792 __constrain_hash(__ndptr->__hash(), __bc) == __chash;
Howard Hinnant3e519522010-05-11 19:42:16 +00001793 __ndptr = __ndptr->__next_)
1794 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001795 if (key_eq()(__ndptr->__upcast()->__value_, __nd->__value_))
Howard Hinnant3e519522010-05-11 19:42:16 +00001796 goto __done;
1797 }
1798 }
1799 }
1800 {
1801 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1802 {
Eric Fiselier9ba5c112015-02-02 21:31:48 +00001803 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnant3e519522010-05-11 19:42:16 +00001804 size_type(ceil(float(size() + 1) / max_load_factor()))));
1805 __bc = bucket_count();
Howard Hinnant4cb38a82012-07-06 17:31:14 +00001806 __chash = __constrain_hash(__nd->__hash_, __bc);
Howard Hinnant3e519522010-05-11 19:42:16 +00001807 }
1808 // insert_after __bucket_list_[__chash], or __first_node if bucket is null
Eric Fiselier40492ba2016-07-23 20:36:55 +00001809 __next_pointer __pn = __bucket_list_[__chash];
Howard Hinnant3e519522010-05-11 19:42:16 +00001810 if (__pn == nullptr)
1811 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001812 __pn =__p1_.first().__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001813 __nd->__next_ = __pn->__next_;
Eric Fiselier40492ba2016-07-23 20:36:55 +00001814 __pn->__next_ = __nd->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001815 // fix up __bucket_list_
1816 __bucket_list_[__chash] = __pn;
1817 if (__nd->__next_ != nullptr)
Eric Fiselier40492ba2016-07-23 20:36:55 +00001818 __bucket_list_[__constrain_hash(__nd->__next_->__hash(), __bc)] = __nd->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001819 }
1820 else
1821 {
1822 __nd->__next_ = __pn->__next_;
Eric Fiselier40492ba2016-07-23 20:36:55 +00001823 __pn->__next_ = __nd->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001824 }
Eric Fiselier40492ba2016-07-23 20:36:55 +00001825 __ndptr = __nd->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001826 // increment size
1827 ++size();
1828 __inserted = true;
1829 }
1830__done:
Howard Hinnantb24c8022013-07-23 22:01:58 +00001831#if _LIBCPP_DEBUG_LEVEL >= 2
1832 return pair<iterator, bool>(iterator(__ndptr, this), __inserted);
1833#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001834 return pair<iterator, bool>(iterator(__ndptr), __inserted);
Howard Hinnantb24c8022013-07-23 22:01:58 +00001835#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001836}
1837
1838template <class _Tp, class _Hash, class _Equal, class _Alloc>
1839typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1840__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi(__node_pointer __cp)
1841{
1842 __cp->__hash_ = hash_function()(__cp->__value_);
1843 size_type __bc = bucket_count();
1844 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1845 {
Eric Fiselier9ba5c112015-02-02 21:31:48 +00001846 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnant3e519522010-05-11 19:42:16 +00001847 size_type(ceil(float(size() + 1) / max_load_factor()))));
1848 __bc = bucket_count();
1849 }
Howard Hinnant4cb38a82012-07-06 17:31:14 +00001850 size_t __chash = __constrain_hash(__cp->__hash_, __bc);
Eric Fiselier40492ba2016-07-23 20:36:55 +00001851 __next_pointer __pn = __bucket_list_[__chash];
Howard Hinnant3e519522010-05-11 19:42:16 +00001852 if (__pn == nullptr)
1853 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001854 __pn =__p1_.first().__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001855 __cp->__next_ = __pn->__next_;
Eric Fiselier40492ba2016-07-23 20:36:55 +00001856 __pn->__next_ = __cp->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001857 // fix up __bucket_list_
1858 __bucket_list_[__chash] = __pn;
1859 if (__cp->__next_ != nullptr)
Eric Fiselier40492ba2016-07-23 20:36:55 +00001860 __bucket_list_[__constrain_hash(__cp->__next_->__hash(), __bc)]
1861 = __cp->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001862 }
1863 else
1864 {
1865 for (bool __found = false; __pn->__next_ != nullptr &&
Eric Fiselier40492ba2016-07-23 20:36:55 +00001866 __constrain_hash(__pn->__next_->__hash(), __bc) == __chash;
Howard Hinnant3e519522010-05-11 19:42:16 +00001867 __pn = __pn->__next_)
Howard Hinnantb3371f62010-08-22 00:02:43 +00001868 {
Howard Hinnant3e519522010-05-11 19:42:16 +00001869 // __found key_eq() action
1870 // false false loop
1871 // true true loop
1872 // false true set __found to true
1873 // true false break
Eric Fiselier40492ba2016-07-23 20:36:55 +00001874 if (__found != (__pn->__next_->__hash() == __cp->__hash_ &&
1875 key_eq()(__pn->__next_->__upcast()->__value_, __cp->__value_)))
Howard Hinnant3e519522010-05-11 19:42:16 +00001876 {
1877 if (!__found)
1878 __found = true;
1879 else
1880 break;
1881 }
1882 }
1883 __cp->__next_ = __pn->__next_;
Eric Fiselier40492ba2016-07-23 20:36:55 +00001884 __pn->__next_ = __cp->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001885 if (__cp->__next_ != nullptr)
1886 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001887 size_t __nhash = __constrain_hash(__cp->__next_->__hash(), __bc);
Howard Hinnant3e519522010-05-11 19:42:16 +00001888 if (__nhash != __chash)
Eric Fiselier40492ba2016-07-23 20:36:55 +00001889 __bucket_list_[__nhash] = __cp->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001890 }
1891 }
1892 ++size();
Howard Hinnantb24c8022013-07-23 22:01:58 +00001893#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselier40492ba2016-07-23 20:36:55 +00001894 return iterator(__cp->__ptr(), this);
Howard Hinnantb24c8022013-07-23 22:01:58 +00001895#else
Eric Fiselier40492ba2016-07-23 20:36:55 +00001896 return iterator(__cp->__ptr());
Howard Hinnantb24c8022013-07-23 22:01:58 +00001897#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001898}
1899
1900template <class _Tp, class _Hash, class _Equal, class _Alloc>
1901typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1902__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi(
1903 const_iterator __p, __node_pointer __cp)
1904{
Howard Hinnant2f51de52013-08-02 17:50:49 +00001905#if _LIBCPP_DEBUG_LEVEL >= 2
1906 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1907 "unordered container::emplace_hint(const_iterator, args...) called with an iterator not"
1908 " referring to this unordered container");
1909#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001910 if (__p != end() && key_eq()(*__p, __cp->__value_))
1911 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001912 __next_pointer __np = __p.__node_;
1913 __cp->__hash_ = __np->__hash();
Howard Hinnant3e519522010-05-11 19:42:16 +00001914 size_type __bc = bucket_count();
1915 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1916 {
Eric Fiselier9ba5c112015-02-02 21:31:48 +00001917 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnant3e519522010-05-11 19:42:16 +00001918 size_type(ceil(float(size() + 1) / max_load_factor()))));
1919 __bc = bucket_count();
1920 }
Howard Hinnant4cb38a82012-07-06 17:31:14 +00001921 size_t __chash = __constrain_hash(__cp->__hash_, __bc);
Eric Fiselier40492ba2016-07-23 20:36:55 +00001922 __next_pointer __pp = __bucket_list_[__chash];
Howard Hinnant3e519522010-05-11 19:42:16 +00001923 while (__pp->__next_ != __np)
1924 __pp = __pp->__next_;
1925 __cp->__next_ = __np;
Eric Fiselier40492ba2016-07-23 20:36:55 +00001926 __pp->__next_ = static_cast<__next_pointer>(__cp);
Howard Hinnant3e519522010-05-11 19:42:16 +00001927 ++size();
Howard Hinnantb24c8022013-07-23 22:01:58 +00001928#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselier40492ba2016-07-23 20:36:55 +00001929 return iterator(static_cast<__next_pointer>(__cp), this);
Howard Hinnantb24c8022013-07-23 22:01:58 +00001930#else
Eric Fiselier40492ba2016-07-23 20:36:55 +00001931 return iterator(static_cast<__next_pointer>(__cp));
Howard Hinnantb24c8022013-07-23 22:01:58 +00001932#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001933 }
1934 return __node_insert_multi(__cp);
1935}
1936
Eric Fiselierde3f2b32015-06-13 07:18:32 +00001937
1938
Eric Fiselierfcd02212016-02-11 11:59:44 +00001939#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierde3f2b32015-06-13 07:18:32 +00001940template <class _Tp, class _Hash, class _Equal, class _Alloc>
Eric Fiselierfcd02212016-02-11 11:59:44 +00001941template <class _Key, class ..._Args>
Eric Fiselierde3f2b32015-06-13 07:18:32 +00001942pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
Eric Fiselierfcd02212016-02-11 11:59:44 +00001943__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_key_args(_Key const& __k, _Args&&... __args)
Eric Fiselierde3f2b32015-06-13 07:18:32 +00001944#else
1945template <class _Tp, class _Hash, class _Equal, class _Alloc>
Eric Fiselierfcd02212016-02-11 11:59:44 +00001946template <class _Key, class _Args>
Eric Fiselierde3f2b32015-06-13 07:18:32 +00001947pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
Eric Fiselierfcd02212016-02-11 11:59:44 +00001948__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_key_args(_Key const& __k, _Args& __args)
Eric Fiselierde3f2b32015-06-13 07:18:32 +00001949#endif
1950{
Eric Fiselierfcd02212016-02-11 11:59:44 +00001951
1952 size_t __hash = hash_function()(__k);
Howard Hinnant3e519522010-05-11 19:42:16 +00001953 size_type __bc = bucket_count();
1954 bool __inserted = false;
Eric Fiselier40492ba2016-07-23 20:36:55 +00001955 __next_pointer __nd;
Howard Hinnant3e519522010-05-11 19:42:16 +00001956 size_t __chash;
1957 if (__bc != 0)
1958 {
Howard Hinnant4cb38a82012-07-06 17:31:14 +00001959 __chash = __constrain_hash(__hash, __bc);
Howard Hinnant3e519522010-05-11 19:42:16 +00001960 __nd = __bucket_list_[__chash];
1961 if (__nd != nullptr)
1962 {
1963 for (__nd = __nd->__next_; __nd != nullptr &&
Eric Fiselier1a06fe52016-07-24 06:22:25 +00001964 (__nd->__hash() == __hash || __constrain_hash(__nd->__hash(), __bc) == __chash);
Howard Hinnant3e519522010-05-11 19:42:16 +00001965 __nd = __nd->__next_)
1966 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001967 if (key_eq()(__nd->__upcast()->__value_, __k))
Howard Hinnant3e519522010-05-11 19:42:16 +00001968 goto __done;
1969 }
1970 }
1971 }
1972 {
Eric Fiselierfcd02212016-02-11 11:59:44 +00001973#ifndef _LIBCPP_CXX03_LANG
1974 __node_holder __h = __construct_node_hash(__hash, _VSTD::forward<_Args>(__args)...);
1975#else
1976 __node_holder __h = __construct_node_hash(__hash, __args);
1977#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001978 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1979 {
Eric Fiselier9ba5c112015-02-02 21:31:48 +00001980 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnant3e519522010-05-11 19:42:16 +00001981 size_type(ceil(float(size() + 1) / max_load_factor()))));
1982 __bc = bucket_count();
Howard Hinnant4cb38a82012-07-06 17:31:14 +00001983 __chash = __constrain_hash(__hash, __bc);
Howard Hinnant3e519522010-05-11 19:42:16 +00001984 }
1985 // insert_after __bucket_list_[__chash], or __first_node if bucket is null
Eric Fiselier40492ba2016-07-23 20:36:55 +00001986 __next_pointer __pn = __bucket_list_[__chash];
Howard Hinnant3e519522010-05-11 19:42:16 +00001987 if (__pn == nullptr)
1988 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001989 __pn = __p1_.first().__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001990 __h->__next_ = __pn->__next_;
Eric Fiselier40492ba2016-07-23 20:36:55 +00001991 __pn->__next_ = __h.get()->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001992 // fix up __bucket_list_
1993 __bucket_list_[__chash] = __pn;
1994 if (__h->__next_ != nullptr)
Eric Fiselier40492ba2016-07-23 20:36:55 +00001995 __bucket_list_[__constrain_hash(__h->__next_->__hash(), __bc)]
1996 = __h.get()->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001997 }
1998 else
1999 {
2000 __h->__next_ = __pn->__next_;
Eric Fiselier40492ba2016-07-23 20:36:55 +00002001 __pn->__next_ = static_cast<__next_pointer>(__h.get());
Howard Hinnant3e519522010-05-11 19:42:16 +00002002 }
Eric Fiselier40492ba2016-07-23 20:36:55 +00002003 __nd = static_cast<__next_pointer>(__h.release());
Howard Hinnant3e519522010-05-11 19:42:16 +00002004 // increment size
2005 ++size();
2006 __inserted = true;
2007 }
2008__done:
Howard Hinnantb24c8022013-07-23 22:01:58 +00002009#if _LIBCPP_DEBUG_LEVEL >= 2
2010 return pair<iterator, bool>(iterator(__nd, this), __inserted);
2011#else
Howard Hinnant3e519522010-05-11 19:42:16 +00002012 return pair<iterator, bool>(iterator(__nd), __inserted);
Howard Hinnantb24c8022013-07-23 22:01:58 +00002013#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002014}
2015
Eric Fiselierfcd02212016-02-11 11:59:44 +00002016#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00002017
2018template <class _Tp, class _Hash, class _Equal, class _Alloc>
2019template <class... _Args>
2020pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
Duncan P. N. Exon Smithfde79b42016-03-17 20:45:20 +00002021__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_impl(_Args&&... __args)
Howard Hinnant3e519522010-05-11 19:42:16 +00002022{
Howard Hinnantce48a112011-06-30 21:18:19 +00002023 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnant3e519522010-05-11 19:42:16 +00002024 pair<iterator, bool> __r = __node_insert_unique(__h.get());
2025 if (__r.second)
2026 __h.release();
2027 return __r;
2028}
2029
2030template <class _Tp, class _Hash, class _Equal, class _Alloc>
2031template <class... _Args>
2032typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2033__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_multi(_Args&&... __args)
2034{
Howard Hinnantce48a112011-06-30 21:18:19 +00002035 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnant3e519522010-05-11 19:42:16 +00002036 iterator __r = __node_insert_multi(__h.get());
2037 __h.release();
2038 return __r;
2039}
2040
2041template <class _Tp, class _Hash, class _Equal, class _Alloc>
2042template <class... _Args>
2043typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2044__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_hint_multi(
2045 const_iterator __p, _Args&&... __args)
2046{
Howard Hinnante5c13de2013-07-29 19:05:47 +00002047#if _LIBCPP_DEBUG_LEVEL >= 2
2048 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2049 "unordered container::emplace_hint(const_iterator, args...) called with an iterator not"
2050 " referring to this unordered container");
2051#endif
Howard Hinnantce48a112011-06-30 21:18:19 +00002052 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnant3e519522010-05-11 19:42:16 +00002053 iterator __r = __node_insert_multi(__p, __h.get());
2054 __h.release();
2055 return __r;
2056}
2057
Eric Fiselierfcd02212016-02-11 11:59:44 +00002058#else // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00002059
2060template <class _Tp, class _Hash, class _Equal, class _Alloc>
2061typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
Eric Fiselierfcd02212016-02-11 11:59:44 +00002062__hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const __container_value_type& __x)
Howard Hinnant3e519522010-05-11 19:42:16 +00002063{
2064 __node_holder __h = __construct_node(__x);
2065 iterator __r = __node_insert_multi(__h.get());
2066 __h.release();
2067 return __r;
2068}
2069
2070template <class _Tp, class _Hash, class _Equal, class _Alloc>
2071typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2072__hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const_iterator __p,
Eric Fiselierfcd02212016-02-11 11:59:44 +00002073 const __container_value_type& __x)
Howard Hinnant3e519522010-05-11 19:42:16 +00002074{
Howard Hinnante5c13de2013-07-29 19:05:47 +00002075#if _LIBCPP_DEBUG_LEVEL >= 2
2076 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2077 "unordered container::insert(const_iterator, lvalue) called with an iterator not"
2078 " referring to this unordered container");
2079#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002080 __node_holder __h = __construct_node(__x);
2081 iterator __r = __node_insert_multi(__p, __h.get());
2082 __h.release();
2083 return __r;
2084}
2085
Eric Fiselierfcd02212016-02-11 11:59:44 +00002086#endif // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00002087
2088template <class _Tp, class _Hash, class _Equal, class _Alloc>
2089void
2090__hash_table<_Tp, _Hash, _Equal, _Alloc>::rehash(size_type __n)
2091{
Howard Hinnant4cb38a82012-07-06 17:31:14 +00002092 if (__n == 1)
2093 __n = 2;
2094 else if (__n & (__n - 1))
2095 __n = __next_prime(__n);
Howard Hinnant3e519522010-05-11 19:42:16 +00002096 size_type __bc = bucket_count();
2097 if (__n > __bc)
2098 __rehash(__n);
Howard Hinnant4cb38a82012-07-06 17:31:14 +00002099 else if (__n < __bc)
Howard Hinnant3e519522010-05-11 19:42:16 +00002100 {
Howard Hinnantce48a112011-06-30 21:18:19 +00002101 __n = _VSTD::max<size_type>
Howard Hinnant3e519522010-05-11 19:42:16 +00002102 (
2103 __n,
Eric Fiselier9ba5c112015-02-02 21:31:48 +00002104 __is_hash_power2(__bc) ? __next_hash_pow2(size_t(ceil(float(size()) / max_load_factor()))) :
2105 __next_prime(size_t(ceil(float(size()) / max_load_factor())))
Howard Hinnant3e519522010-05-11 19:42:16 +00002106 );
2107 if (__n < __bc)
2108 __rehash(__n);
2109 }
2110}
2111
2112template <class _Tp, class _Hash, class _Equal, class _Alloc>
2113void
2114__hash_table<_Tp, _Hash, _Equal, _Alloc>::__rehash(size_type __nbc)
2115{
Howard Hinnantb24c8022013-07-23 22:01:58 +00002116#if _LIBCPP_DEBUG_LEVEL >= 2
2117 __get_db()->__invalidate_all(this);
2118#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant3e519522010-05-11 19:42:16 +00002119 __pointer_allocator& __npa = __bucket_list_.get_deleter().__alloc();
2120 __bucket_list_.reset(__nbc > 0 ?
2121 __pointer_alloc_traits::allocate(__npa, __nbc) : nullptr);
2122 __bucket_list_.get_deleter().size() = __nbc;
2123 if (__nbc > 0)
2124 {
2125 for (size_type __i = 0; __i < __nbc; ++__i)
2126 __bucket_list_[__i] = nullptr;
Eric Fiselier40492ba2016-07-23 20:36:55 +00002127 __next_pointer __pp = __p1_.first().__ptr();
2128 __next_pointer __cp = __pp->__next_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002129 if (__cp != nullptr)
2130 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00002131 size_type __chash = __constrain_hash(__cp->__hash(), __nbc);
Howard Hinnant3e519522010-05-11 19:42:16 +00002132 __bucket_list_[__chash] = __pp;
2133 size_type __phash = __chash;
2134 for (__pp = __cp, __cp = __cp->__next_; __cp != nullptr;
2135 __cp = __pp->__next_)
2136 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00002137 __chash = __constrain_hash(__cp->__hash(), __nbc);
Howard Hinnant3e519522010-05-11 19:42:16 +00002138 if (__chash == __phash)
2139 __pp = __cp;
2140 else
2141 {
2142 if (__bucket_list_[__chash] == nullptr)
2143 {
2144 __bucket_list_[__chash] = __pp;
2145 __pp = __cp;
2146 __phash = __chash;
2147 }
2148 else
2149 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00002150 __next_pointer __np = __cp;
Howard Hinnant3e519522010-05-11 19:42:16 +00002151 for (; __np->__next_ != nullptr &&
Eric Fiselier40492ba2016-07-23 20:36:55 +00002152 key_eq()(__cp->__upcast()->__value_,
2153 __np->__next_->__upcast()->__value_);
Howard Hinnant3e519522010-05-11 19:42:16 +00002154 __np = __np->__next_)
2155 ;
2156 __pp->__next_ = __np->__next_;
2157 __np->__next_ = __bucket_list_[__chash]->__next_;
2158 __bucket_list_[__chash]->__next_ = __cp;
Howard Hinnantb3371f62010-08-22 00:02:43 +00002159
Howard Hinnant3e519522010-05-11 19:42:16 +00002160 }
2161 }
2162 }
2163 }
2164 }
2165}
2166
2167template <class _Tp, class _Hash, class _Equal, class _Alloc>
2168template <class _Key>
2169typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2170__hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k)
2171{
2172 size_t __hash = hash_function()(__k);
2173 size_type __bc = bucket_count();
2174 if (__bc != 0)
2175 {
Howard Hinnant4cb38a82012-07-06 17:31:14 +00002176 size_t __chash = __constrain_hash(__hash, __bc);
Eric Fiselier40492ba2016-07-23 20:36:55 +00002177 __next_pointer __nd = __bucket_list_[__chash];
Howard Hinnant3e519522010-05-11 19:42:16 +00002178 if (__nd != nullptr)
2179 {
2180 for (__nd = __nd->__next_; __nd != nullptr &&
Eric Fiselier40492ba2016-07-23 20:36:55 +00002181 (__nd->__hash() == __hash
2182 || __constrain_hash(__nd->__hash(), __bc) == __chash);
Howard Hinnant3e519522010-05-11 19:42:16 +00002183 __nd = __nd->__next_)
2184 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00002185 if ((__nd->__hash() == __hash)
2186 && key_eq()(__nd->__upcast()->__value_, __k))
Howard Hinnantb24c8022013-07-23 22:01:58 +00002187#if _LIBCPP_DEBUG_LEVEL >= 2
2188 return iterator(__nd, this);
2189#else
Howard Hinnant3e519522010-05-11 19:42:16 +00002190 return iterator(__nd);
Howard Hinnantb24c8022013-07-23 22:01:58 +00002191#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002192 }
2193 }
2194 }
2195 return end();
2196}
2197
2198template <class _Tp, class _Hash, class _Equal, class _Alloc>
2199template <class _Key>
2200typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
2201__hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k) const
2202{
2203 size_t __hash = hash_function()(__k);
2204 size_type __bc = bucket_count();
2205 if (__bc != 0)
2206 {
Howard Hinnant4cb38a82012-07-06 17:31:14 +00002207 size_t __chash = __constrain_hash(__hash, __bc);
Eric Fiselier40492ba2016-07-23 20:36:55 +00002208 __next_pointer __nd = __bucket_list_[__chash];
Howard Hinnant3e519522010-05-11 19:42:16 +00002209 if (__nd != nullptr)
2210 {
2211 for (__nd = __nd->__next_; __nd != nullptr &&
Eric Fiselier40492ba2016-07-23 20:36:55 +00002212 (__hash == __nd->__hash()
2213 || __constrain_hash(__nd->__hash(), __bc) == __chash);
Howard Hinnant3e519522010-05-11 19:42:16 +00002214 __nd = __nd->__next_)
2215 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00002216 if ((__nd->__hash() == __hash)
2217 && key_eq()(__nd->__upcast()->__value_, __k))
Howard Hinnantb24c8022013-07-23 22:01:58 +00002218#if _LIBCPP_DEBUG_LEVEL >= 2
2219 return const_iterator(__nd, this);
2220#else
Howard Hinnant3e519522010-05-11 19:42:16 +00002221 return const_iterator(__nd);
Howard Hinnantb24c8022013-07-23 22:01:58 +00002222#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002223 }
2224 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00002225
Howard Hinnant3e519522010-05-11 19:42:16 +00002226 }
2227 return end();
2228}
2229
Eric Fiselierfcd02212016-02-11 11:59:44 +00002230#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00002231
2232template <class _Tp, class _Hash, class _Equal, class _Alloc>
2233template <class ..._Args>
2234typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
2235__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(_Args&& ...__args)
2236{
Eric Fiselierfcd02212016-02-11 11:59:44 +00002237 static_assert(!__is_hash_value_type<_Args...>::value,
2238 "Construct cannot be called with a hash value type");
Howard Hinnant3e519522010-05-11 19:42:16 +00002239 __node_allocator& __na = __node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00002240 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselierfcd02212016-02-11 11:59:44 +00002241 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), _VSTD::forward<_Args>(__args)...);
Howard Hinnant3e519522010-05-11 19:42:16 +00002242 __h.get_deleter().__value_constructed = true;
2243 __h->__hash_ = hash_function()(__h->__value_);
2244 __h->__next_ = nullptr;
2245 return __h;
2246}
2247
2248template <class _Tp, class _Hash, class _Equal, class _Alloc>
Eric Fiselierfcd02212016-02-11 11:59:44 +00002249template <class _First, class ..._Rest>
Howard Hinnant3e519522010-05-11 19:42:16 +00002250typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Eric Fiselierfcd02212016-02-11 11:59:44 +00002251__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node_hash(
2252 size_t __hash, _First&& __f, _Rest&& ...__rest)
Howard Hinnant3e519522010-05-11 19:42:16 +00002253{
Eric Fiselierfcd02212016-02-11 11:59:44 +00002254 static_assert(!__is_hash_value_type<_First, _Rest...>::value,
2255 "Construct cannot be called with a hash value type");
Howard Hinnant3e519522010-05-11 19:42:16 +00002256 __node_allocator& __na = __node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00002257 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselierfcd02212016-02-11 11:59:44 +00002258 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_),
2259 _VSTD::forward<_First>(__f),
2260 _VSTD::forward<_Rest>(__rest)...);
Howard Hinnant3e519522010-05-11 19:42:16 +00002261 __h.get_deleter().__value_constructed = true;
2262 __h->__hash_ = __hash;
2263 __h->__next_ = nullptr;
Howard Hinnant179b1f82013-08-22 18:29:50 +00002264 return __h;
Howard Hinnant3e519522010-05-11 19:42:16 +00002265}
2266
Eric Fiselierfcd02212016-02-11 11:59:44 +00002267#else // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00002268
2269template <class _Tp, class _Hash, class _Equal, class _Alloc>
2270typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Eric Fiselierfcd02212016-02-11 11:59:44 +00002271__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(const __container_value_type& __v)
Howard Hinnant3e519522010-05-11 19:42:16 +00002272{
2273 __node_allocator& __na = __node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00002274 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselierfcd02212016-02-11 11:59:44 +00002275 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), __v);
Howard Hinnant3e519522010-05-11 19:42:16 +00002276 __h.get_deleter().__value_constructed = true;
2277 __h->__hash_ = hash_function()(__h->__value_);
2278 __h->__next_ = nullptr;
Dimitry Andric251c6292015-08-19 06:43:33 +00002279 return _LIBCPP_EXPLICIT_MOVE(__h); // explicitly moved for C++03
Howard Hinnant3e519522010-05-11 19:42:16 +00002280}
2281
Howard Hinnant3e519522010-05-11 19:42:16 +00002282template <class _Tp, class _Hash, class _Equal, class _Alloc>
2283typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Eric Fiselierfcd02212016-02-11 11:59:44 +00002284__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node_hash(size_t __hash,
2285 const __container_value_type& __v)
Howard Hinnant3e519522010-05-11 19:42:16 +00002286{
2287 __node_allocator& __na = __node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00002288 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselierfcd02212016-02-11 11:59:44 +00002289 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), __v);
Howard Hinnant3e519522010-05-11 19:42:16 +00002290 __h.get_deleter().__value_constructed = true;
2291 __h->__hash_ = __hash;
2292 __h->__next_ = nullptr;
Dimitry Andric251c6292015-08-19 06:43:33 +00002293 return _LIBCPP_EXPLICIT_MOVE(__h); // explicitly moved for C++03
Howard Hinnant3e519522010-05-11 19:42:16 +00002294}
2295
Eric Fiselierfcd02212016-02-11 11:59:44 +00002296#endif // _LIBCPP_CXX03_LANG
2297
Howard Hinnant3e519522010-05-11 19:42:16 +00002298template <class _Tp, class _Hash, class _Equal, class _Alloc>
2299typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2300__hash_table<_Tp, _Hash, _Equal, _Alloc>::erase(const_iterator __p)
2301{
Eric Fiselier40492ba2016-07-23 20:36:55 +00002302 __next_pointer __np = __p.__node_;
Howard Hinnantb24c8022013-07-23 22:01:58 +00002303#if _LIBCPP_DEBUG_LEVEL >= 2
2304 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2305 "unordered container erase(iterator) called with an iterator not"
2306 " referring to this container");
2307 _LIBCPP_ASSERT(__p != end(),
2308 "unordered container erase(iterator) called with a non-dereferenceable iterator");
2309 iterator __r(__np, this);
2310#else
Howard Hinnant3e519522010-05-11 19:42:16 +00002311 iterator __r(__np);
Howard Hinnantb24c8022013-07-23 22:01:58 +00002312#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002313 ++__r;
2314 remove(__p);
2315 return __r;
2316}
2317
2318template <class _Tp, class _Hash, class _Equal, class _Alloc>
2319typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2320__hash_table<_Tp, _Hash, _Equal, _Alloc>::erase(const_iterator __first,
2321 const_iterator __last)
2322{
Howard Hinnantb24c8022013-07-23 22:01:58 +00002323#if _LIBCPP_DEBUG_LEVEL >= 2
2324 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
2325 "unodered container::erase(iterator, iterator) called with an iterator not"
2326 " referring to this unodered container");
2327 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__last) == this,
2328 "unodered container::erase(iterator, iterator) called with an iterator not"
2329 " referring to this unodered container");
2330#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002331 for (const_iterator __p = __first; __first != __last; __p = __first)
2332 {
2333 ++__first;
2334 erase(__p);
2335 }
Eric Fiselier40492ba2016-07-23 20:36:55 +00002336 __next_pointer __np = __last.__node_;
Howard Hinnantb24c8022013-07-23 22:01:58 +00002337#if _LIBCPP_DEBUG_LEVEL >= 2
2338 return iterator (__np, this);
2339#else
Howard Hinnant3e519522010-05-11 19:42:16 +00002340 return iterator (__np);
Howard Hinnantb24c8022013-07-23 22:01:58 +00002341#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002342}
2343
2344template <class _Tp, class _Hash, class _Equal, class _Alloc>
2345template <class _Key>
2346typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2347__hash_table<_Tp, _Hash, _Equal, _Alloc>::__erase_unique(const _Key& __k)
2348{
2349 iterator __i = find(__k);
2350 if (__i == end())
2351 return 0;
2352 erase(__i);
2353 return 1;
2354}
2355
2356template <class _Tp, class _Hash, class _Equal, class _Alloc>
2357template <class _Key>
2358typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2359__hash_table<_Tp, _Hash, _Equal, _Alloc>::__erase_multi(const _Key& __k)
2360{
2361 size_type __r = 0;
2362 iterator __i = find(__k);
2363 if (__i != end())
2364 {
2365 iterator __e = end();
2366 do
2367 {
2368 erase(__i++);
2369 ++__r;
2370 } while (__i != __e && key_eq()(*__i, __k));
2371 }
2372 return __r;
2373}
2374
2375template <class _Tp, class _Hash, class _Equal, class _Alloc>
2376typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Howard Hinnant37141072011-06-04 18:54:24 +00002377__hash_table<_Tp, _Hash, _Equal, _Alloc>::remove(const_iterator __p) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00002378{
2379 // current node
Eric Fiselier40492ba2016-07-23 20:36:55 +00002380 __next_pointer __cn = __p.__node_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002381 size_type __bc = bucket_count();
Eric Fiselier40492ba2016-07-23 20:36:55 +00002382 size_t __chash = __constrain_hash(__cn->__hash(), __bc);
Howard Hinnant3e519522010-05-11 19:42:16 +00002383 // find previous node
Eric Fiselier40492ba2016-07-23 20:36:55 +00002384 __next_pointer __pn = __bucket_list_[__chash];
Howard Hinnant3e519522010-05-11 19:42:16 +00002385 for (; __pn->__next_ != __cn; __pn = __pn->__next_)
2386 ;
2387 // Fix up __bucket_list_
2388 // if __pn is not in same bucket (before begin is not in same bucket) &&
2389 // if __cn->__next_ is not in same bucket (nullptr is not in same bucket)
Eric Fiselier40492ba2016-07-23 20:36:55 +00002390 if (__pn == __p1_.first().__ptr()
2391 || __constrain_hash(__pn->__hash(), __bc) != __chash)
Howard Hinnant3e519522010-05-11 19:42:16 +00002392 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00002393 if (__cn->__next_ == nullptr
2394 || __constrain_hash(__cn->__next_->__hash(), __bc) != __chash)
Howard Hinnant3e519522010-05-11 19:42:16 +00002395 __bucket_list_[__chash] = nullptr;
2396 }
2397 // if __cn->__next_ is not in same bucket (nullptr is in same bucket)
2398 if (__cn->__next_ != nullptr)
2399 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00002400 size_t __nhash = __constrain_hash(__cn->__next_->__hash(), __bc);
Howard Hinnant3e519522010-05-11 19:42:16 +00002401 if (__nhash != __chash)
2402 __bucket_list_[__nhash] = __pn;
2403 }
2404 // remove __cn
2405 __pn->__next_ = __cn->__next_;
2406 __cn->__next_ = nullptr;
2407 --size();
Howard Hinnantb24c8022013-07-23 22:01:58 +00002408#if _LIBCPP_DEBUG_LEVEL >= 2
2409 __c_node* __c = __get_db()->__find_c_and_lock(this);
Eric Fiselier780b51d2016-12-28 05:53:01 +00002410 for (__i_node** __dp = __c->end_; __dp != __c->beg_; )
Howard Hinnantb24c8022013-07-23 22:01:58 +00002411 {
Eric Fiselier780b51d2016-12-28 05:53:01 +00002412 --__dp;
2413 iterator* __i = static_cast<iterator*>((*__dp)->__i_);
Howard Hinnantb24c8022013-07-23 22:01:58 +00002414 if (__i->__node_ == __cn)
2415 {
Eric Fiselier780b51d2016-12-28 05:53:01 +00002416 (*__dp)->__c_ = nullptr;
2417 if (--__c->end_ != __dp)
2418 memmove(__dp, __dp+1, (__c->end_ - __dp)*sizeof(__i_node*));
Howard Hinnantb24c8022013-07-23 22:01:58 +00002419 }
2420 }
2421 __get_db()->unlock();
2422#endif
Eric Fiselier40492ba2016-07-23 20:36:55 +00002423 return __node_holder(__cn->__upcast(), _Dp(__node_alloc(), true));
Howard Hinnant3e519522010-05-11 19:42:16 +00002424}
2425
2426template <class _Tp, class _Hash, class _Equal, class _Alloc>
2427template <class _Key>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00002428inline
Howard Hinnant3e519522010-05-11 19:42:16 +00002429typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2430__hash_table<_Tp, _Hash, _Equal, _Alloc>::__count_unique(const _Key& __k) const
2431{
2432 return static_cast<size_type>(find(__k) != end());
2433}
2434
2435template <class _Tp, class _Hash, class _Equal, class _Alloc>
2436template <class _Key>
2437typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2438__hash_table<_Tp, _Hash, _Equal, _Alloc>::__count_multi(const _Key& __k) const
2439{
2440 size_type __r = 0;
2441 const_iterator __i = find(__k);
2442 if (__i != end())
2443 {
2444 const_iterator __e = end();
2445 do
2446 {
2447 ++__i;
2448 ++__r;
2449 } while (__i != __e && key_eq()(*__i, __k));
2450 }
2451 return __r;
2452}
2453
2454template <class _Tp, class _Hash, class _Equal, class _Alloc>
2455template <class _Key>
2456pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator,
2457 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator>
2458__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_unique(
2459 const _Key& __k)
2460{
2461 iterator __i = find(__k);
2462 iterator __j = __i;
2463 if (__i != end())
2464 ++__j;
2465 return pair<iterator, iterator>(__i, __j);
2466}
2467
2468template <class _Tp, class _Hash, class _Equal, class _Alloc>
2469template <class _Key>
2470pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator,
2471 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator>
2472__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_unique(
2473 const _Key& __k) const
2474{
2475 const_iterator __i = find(__k);
2476 const_iterator __j = __i;
2477 if (__i != end())
2478 ++__j;
2479 return pair<const_iterator, const_iterator>(__i, __j);
2480}
2481
2482template <class _Tp, class _Hash, class _Equal, class _Alloc>
2483template <class _Key>
2484pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator,
2485 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator>
2486__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_multi(
2487 const _Key& __k)
2488{
2489 iterator __i = find(__k);
2490 iterator __j = __i;
2491 if (__i != end())
2492 {
2493 iterator __e = end();
2494 do
2495 {
2496 ++__j;
2497 } while (__j != __e && key_eq()(*__j, __k));
2498 }
2499 return pair<iterator, iterator>(__i, __j);
2500}
2501
2502template <class _Tp, class _Hash, class _Equal, class _Alloc>
2503template <class _Key>
2504pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator,
2505 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator>
2506__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_multi(
2507 const _Key& __k) const
2508{
2509 const_iterator __i = find(__k);
2510 const_iterator __j = __i;
2511 if (__i != end())
2512 {
2513 const_iterator __e = end();
2514 do
2515 {
2516 ++__j;
2517 } while (__j != __e && key_eq()(*__j, __k));
2518 }
2519 return pair<const_iterator, const_iterator>(__i, __j);
2520}
2521
2522template <class _Tp, class _Hash, class _Equal, class _Alloc>
2523void
2524__hash_table<_Tp, _Hash, _Equal, _Alloc>::swap(__hash_table& __u)
Eric Fiselier87a82492015-07-18 20:40:46 +00002525#if _LIBCPP_STD_VER <= 11
Eric Fiselier780b51d2016-12-28 05:53:01 +00002526 _NOEXCEPT_DEBUG_(
Marshall Clowe3fbe142015-07-13 20:04:56 +00002527 __is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value
Marshall Clowe3fbe142015-07-13 20:04:56 +00002528 && (!allocator_traits<__pointer_allocator>::propagate_on_container_swap::value
2529 || __is_nothrow_swappable<__pointer_allocator>::value)
2530 && (!__node_traits::propagate_on_container_swap::value
2531 || __is_nothrow_swappable<__node_allocator>::value)
Marshall Clowe3fbe142015-07-13 20:04:56 +00002532 )
Eric Fiselier87a82492015-07-18 20:40:46 +00002533#else
Eric Fiselier780b51d2016-12-28 05:53:01 +00002534 _NOEXCEPT_DEBUG_(__is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value)
Eric Fiselier87a82492015-07-18 20:40:46 +00002535#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002536{
Eric Fiselier780b51d2016-12-28 05:53:01 +00002537 _LIBCPP_ASSERT(__node_traits::propagate_on_container_swap::value ||
2538 this->__node_alloc() == __u.__node_alloc(),
2539 "list::swap: Either propagate_on_container_swap must be true"
2540 " or the allocators must compare equal");
Howard Hinnant3e519522010-05-11 19:42:16 +00002541 {
2542 __node_pointer_pointer __npp = __bucket_list_.release();
2543 __bucket_list_.reset(__u.__bucket_list_.release());
2544 __u.__bucket_list_.reset(__npp);
2545 }
Howard Hinnantce48a112011-06-30 21:18:19 +00002546 _VSTD::swap(__bucket_list_.get_deleter().size(), __u.__bucket_list_.get_deleter().size());
Marshall Clowe3fbe142015-07-13 20:04:56 +00002547 __swap_allocator(__bucket_list_.get_deleter().__alloc(),
Howard Hinnant3e519522010-05-11 19:42:16 +00002548 __u.__bucket_list_.get_deleter().__alloc());
Marshall Clowe3fbe142015-07-13 20:04:56 +00002549 __swap_allocator(__node_alloc(), __u.__node_alloc());
Howard Hinnantce48a112011-06-30 21:18:19 +00002550 _VSTD::swap(__p1_.first().__next_, __u.__p1_.first().__next_);
Howard Hinnant3e519522010-05-11 19:42:16 +00002551 __p2_.swap(__u.__p2_);
2552 __p3_.swap(__u.__p3_);
2553 if (size() > 0)
Eric Fiselier40492ba2016-07-23 20:36:55 +00002554 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash(), bucket_count())] =
2555 __p1_.first().__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00002556 if (__u.size() > 0)
Eric Fiselier40492ba2016-07-23 20:36:55 +00002557 __u.__bucket_list_[__constrain_hash(__u.__p1_.first().__next_->__hash(), __u.bucket_count())] =
2558 __u.__p1_.first().__ptr();
Howard Hinnantb24c8022013-07-23 22:01:58 +00002559#if _LIBCPP_DEBUG_LEVEL >= 2
2560 __get_db()->swap(this, &__u);
2561#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002562}
2563
2564template <class _Tp, class _Hash, class _Equal, class _Alloc>
2565typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2566__hash_table<_Tp, _Hash, _Equal, _Alloc>::bucket_size(size_type __n) const
2567{
Howard Hinnante5c13de2013-07-29 19:05:47 +00002568 _LIBCPP_ASSERT(__n < bucket_count(),
2569 "unordered container::bucket_size(n) called with n >= bucket_count()");
Eric Fiselier40492ba2016-07-23 20:36:55 +00002570 __next_pointer __np = __bucket_list_[__n];
Howard Hinnant3e519522010-05-11 19:42:16 +00002571 size_type __bc = bucket_count();
2572 size_type __r = 0;
2573 if (__np != nullptr)
2574 {
2575 for (__np = __np->__next_; __np != nullptr &&
Eric Fiselier40492ba2016-07-23 20:36:55 +00002576 __constrain_hash(__np->__hash(), __bc) == __n;
Howard Hinnant3e519522010-05-11 19:42:16 +00002577 __np = __np->__next_, ++__r)
2578 ;
2579 }
2580 return __r;
2581}
2582
Howard Hinnant37141072011-06-04 18:54:24 +00002583template <class _Tp, class _Hash, class _Equal, class _Alloc>
2584inline _LIBCPP_INLINE_VISIBILITY
2585void
2586swap(__hash_table<_Tp, _Hash, _Equal, _Alloc>& __x,
2587 __hash_table<_Tp, _Hash, _Equal, _Alloc>& __y)
2588 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
2589{
2590 __x.swap(__y);
2591}
2592
Howard Hinnantb24c8022013-07-23 22:01:58 +00002593#if _LIBCPP_DEBUG_LEVEL >= 2
2594
2595template <class _Tp, class _Hash, class _Equal, class _Alloc>
2596bool
2597__hash_table<_Tp, _Hash, _Equal, _Alloc>::__dereferenceable(const const_iterator* __i) const
2598{
2599 return __i->__node_ != nullptr;
2600}
2601
2602template <class _Tp, class _Hash, class _Equal, class _Alloc>
2603bool
2604__hash_table<_Tp, _Hash, _Equal, _Alloc>::__decrementable(const const_iterator*) const
2605{
2606 return false;
2607}
2608
2609template <class _Tp, class _Hash, class _Equal, class _Alloc>
2610bool
2611__hash_table<_Tp, _Hash, _Equal, _Alloc>::__addable(const const_iterator*, ptrdiff_t) const
2612{
2613 return false;
2614}
2615
2616template <class _Tp, class _Hash, class _Equal, class _Alloc>
2617bool
2618__hash_table<_Tp, _Hash, _Equal, _Alloc>::__subscriptable(const const_iterator*, ptrdiff_t) const
2619{
2620 return false;
2621}
2622
2623#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant3e519522010-05-11 19:42:16 +00002624_LIBCPP_END_NAMESPACE_STD
2625
2626#endif // _LIBCPP__HASH_TABLE