blob: 5f21de4ceac85809738120f8062027673fbf49c1 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-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 Hinnantbc8d3f92010-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 Fiselier774c7c52016-02-10 20:46:23 +000020#include <utility>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000021
Howard Hinnant66c6f972011-11-29 16:45:27 +000022#include <__undef_min_max>
Saleem Abdulrasoolf1b30c42015-02-13 22:15:32 +000023#include <__undef___deallocate>
Howard Hinnant66c6f972011-11-29 16:45:27 +000024
Eric Fiselierb9536102014-08-10 23:53:08 +000025#include <__debug>
Howard Hinnant8b00e6c2013-08-02 00:26:35 +000026
Howard Hinnant08e17472011-10-17 20:05:10 +000027#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000028#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +000029#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000030
31_LIBCPP_BEGIN_NAMESPACE_STD
32
Howard Hinnant83eade62013-03-06 23:30:19 +000033_LIBCPP_FUNC_VIS
Howard Hinnant2b1b2d42011-06-14 19:58:17 +000034size_t __next_prime(size_t __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000035
36template <class _NodePtr>
37struct __hash_node_base
38{
39 typedef __hash_node_base __first_node;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000040
Howard Hinnantdf85e572011-02-27 18:02:02 +000041 _NodePtr __next_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000042
Howard Hinnant5f2f14c2011-06-04 18:54:24 +000043 _LIBCPP_INLINE_VISIBILITY __hash_node_base() _NOEXCEPT : __next_(nullptr) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000044};
45
46template <class _Tp, class _VoidPtr>
47struct __hash_node
48 : public __hash_node_base
49 <
Eric Fiselier5cf84e02015-12-30 21:52:00 +000050 typename __rebind_pointer<_VoidPtr, __hash_node<_Tp, _VoidPtr> >::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000051 >
52{
Eric Fiselier774c7c52016-02-10 20:46:23 +000053 typedef _Tp __node_value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000054
55 size_t __hash_;
Eric Fiselier774c7c52016-02-10 20:46:23 +000056 __node_value_type __value_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000057};
58
Howard Hinnant7a445152012-07-06 17:31:14 +000059inline _LIBCPP_INLINE_VISIBILITY
60bool
Eric Fiselier57947ca2015-02-02 21:31:48 +000061__is_hash_power2(size_t __bc)
Howard Hinnant7a445152012-07-06 17:31:14 +000062{
63 return __bc > 2 && !(__bc & (__bc - 1));
64}
65
66inline _LIBCPP_INLINE_VISIBILITY
67size_t
68__constrain_hash(size_t __h, size_t __bc)
69{
70 return !(__bc & (__bc - 1)) ? __h & (__bc - 1) : __h % __bc;
71}
72
73inline _LIBCPP_INLINE_VISIBILITY
74size_t
Eric Fiselier57947ca2015-02-02 21:31:48 +000075__next_hash_pow2(size_t __n)
Howard Hinnant7a445152012-07-06 17:31:14 +000076{
77 return size_t(1) << (std::numeric_limits<size_t>::digits - __clz(__n-1));
78}
79
Howard Hinnant2b1b2d42011-06-14 19:58:17 +000080template <class _Tp, class _Hash, class _Equal, class _Alloc> class __hash_table;
Eric Fiselier774c7c52016-02-10 20:46:23 +000081
82template <class _NodePtr> class _LIBCPP_TYPE_VIS_ONLY __hash_iterator;
Howard Hinnant0f678bd2013-08-12 18:38:34 +000083template <class _ConstNodePtr> class _LIBCPP_TYPE_VIS_ONLY __hash_const_iterator;
Eric Fiselier774c7c52016-02-10 20:46:23 +000084template <class _NodePtr> class _LIBCPP_TYPE_VIS_ONLY __hash_local_iterator;
85template <class _ConstNodePtr> class _LIBCPP_TYPE_VIS_ONLY __hash_const_local_iterator;
Howard Hinnant0f678bd2013-08-12 18:38:34 +000086template <class _HashIterator> class _LIBCPP_TYPE_VIS_ONLY __hash_map_iterator;
87template <class _HashIterator> class _LIBCPP_TYPE_VIS_ONLY __hash_map_const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000088
Eric Fiselier774c7c52016-02-10 20:46:23 +000089#if __cplusplus >= 201103L
90template <class _Key, class _Tp>
91union __hash_value_type;
92#else
93template <class _Key, class _Tp>
94struct __hash_value_type;
95#endif
96
97template <class _Tp>
98struct __key_value_types {
99 static_assert(!is_reference<_Tp>::value && !is_const<_Tp>::value, "");
100 typedef _Tp key_type;
101 typedef _Tp __node_value_type;
102 typedef _Tp __container_value_type;
103 static const bool __is_map = false;
104};
105
106template <class _Key, class _Tp>
107struct __key_value_types<__hash_value_type<_Key, _Tp> > {
108 typedef _Key key_type;
109 typedef _Tp mapped_type;
110 typedef __hash_value_type<_Key, _Tp> __node_value_type;
111 typedef pair<const _Key, _Tp> __container_value_type;
112 typedef __container_value_type __map_value_type;
113 static const bool __is_map = true;
114};
115
116template <class _Tp, class _AllocPtr, class _KVTypes = __key_value_types<_Tp>,
117 bool = _KVTypes::__is_map>
118struct __map_pointer_types {};
119
120template <class _Tp, class _AllocPtr, class _KVTypes>
121struct __map_pointer_types<_Tp, _AllocPtr, _KVTypes, true> {
122 typedef typename _KVTypes::__map_value_type _Mv;
123 typedef typename __rebind_pointer<_AllocPtr, _Mv>::type
124 __map_value_type_pointer;
125 typedef typename __rebind_pointer<_AllocPtr, const _Mv>::type
126 __const_map_value_type_pointer;
127};
128
129template <class _NodePtr, class _NodeT = typename pointer_traits<_NodePtr>::element_type>
130struct __hash_node_types;
131
132template <class _NodePtr, class _Tp, class _VoidPtr>
133struct __hash_node_types<_NodePtr, __hash_node<_Tp, _VoidPtr> >
134 : public __key_value_types<_Tp>, __map_pointer_types<_Tp, _VoidPtr>
135
136{
137 typedef __key_value_types<_Tp> __base;
138
139public:
140 typedef ptrdiff_t difference_type;
141 typedef size_t size_type;
142
143 typedef typename __rebind_pointer<_NodePtr, void>::type __void_pointer;
144
145 typedef typename pointer_traits<_NodePtr>::element_type __node_type;
146 typedef _NodePtr __node_pointer;
147
148 typedef __hash_node_base<__node_pointer> __node_base_type;
149 typedef typename __rebind_pointer<_NodePtr, __node_base_type>::type
150 __node_base_pointer;
151
152 typedef _Tp __node_value_type;
153 typedef typename __rebind_pointer<_VoidPtr, __node_value_type>::type
154 __node_value_type_pointer;
155 typedef typename __rebind_pointer<_VoidPtr, const __node_value_type>::type
156 __const_node_value_type_pointer;
157private:
158 static_assert(!is_const<__node_type>::value,
159 "_NodePtr should never be a pointer to const");
160 static_assert((is_same<typename pointer_traits<_VoidPtr>::element_type, void>::value),
161 "_VoidPtr does not point to unqualified void type");
162 static_assert((is_same<typename __rebind_pointer<_VoidPtr, __node_type>::type,
163 _NodePtr>::value), "_VoidPtr does not rebind to _NodePtr.");
164};
165
166
167
168template <class _HashIterator>
169struct __hash_node_types_from_iterator;
170template <class _NodePtr>
171struct __hash_node_types_from_iterator<__hash_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
172template <class _NodePtr>
173struct __hash_node_types_from_iterator<__hash_const_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
174template <class _NodePtr>
175struct __hash_node_types_from_iterator<__hash_local_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
176template <class _NodePtr>
177struct __hash_node_types_from_iterator<__hash_const_local_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
178
179
180template <class _NodeValueTp, class _VoidPtr>
181struct __make_hash_node_types {
182 typedef __hash_node<_NodeValueTp, _VoidPtr> _NodeTp;
183 typedef typename __rebind_pointer<_VoidPtr, _NodeTp>::type _NodePtr;
184 typedef __hash_node_types<_NodePtr> type;
185};
186
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000187template <class _NodePtr>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000188class _LIBCPP_TYPE_VIS_ONLY __hash_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000189{
Eric Fiselier774c7c52016-02-10 20:46:23 +0000190 typedef __hash_node_types<_NodePtr> _NodeTypes;
191 typedef _NodePtr __node_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000192
193 __node_pointer __node_;
194
195public:
Eric Fiselier774c7c52016-02-10 20:46:23 +0000196 typedef forward_iterator_tag iterator_category;
197 typedef typename _NodeTypes::__node_value_type value_type;
198 typedef typename _NodeTypes::difference_type difference_type;
199 typedef value_type& reference;
200 typedef typename _NodeTypes::__node_value_type_pointer pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000201
Howard Hinnant39213642013-07-23 22:01:58 +0000202 _LIBCPP_INLINE_VISIBILITY __hash_iterator() _NOEXCEPT
Marshall Clow193ef032013-08-07 21:30:44 +0000203#if _LIBCPP_STD_VER > 11
204 : __node_(nullptr)
205#endif
Howard Hinnant39213642013-07-23 22:01:58 +0000206 {
207#if _LIBCPP_DEBUG_LEVEL >= 2
208 __get_db()->__insert_i(this);
209#endif
210 }
211
212#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000213
Howard Hinnant99acc502010-09-21 17:32:39 +0000214 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000215 __hash_iterator(const __hash_iterator& __i)
216 : __node_(__i.__node_)
217 {
218 __get_db()->__iterator_copy(this, &__i);
219 }
220
Howard Hinnant99acc502010-09-21 17:32:39 +0000221 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000222 ~__hash_iterator()
223 {
224 __get_db()->__erase_i(this);
225 }
226
227 _LIBCPP_INLINE_VISIBILITY
228 __hash_iterator& operator=(const __hash_iterator& __i)
229 {
230 if (this != &__i)
231 {
232 __get_db()->__iterator_copy(this, &__i);
233 __node_ = __i.__node_;
234 }
235 return *this;
236 }
237
238#endif // _LIBCPP_DEBUG_LEVEL >= 2
239
240 _LIBCPP_INLINE_VISIBILITY
241 reference operator*() const
242 {
243#if _LIBCPP_DEBUG_LEVEL >= 2
244 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
245 "Attempted to dereference a non-dereferenceable unordered container iterator");
246#endif
247 return __node_->__value_;
248 }
249 _LIBCPP_INLINE_VISIBILITY
250 pointer operator->() const
251 {
252#if _LIBCPP_DEBUG_LEVEL >= 2
253 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
254 "Attempted to dereference a non-dereferenceable unordered container iterator");
255#endif
256 return pointer_traits<pointer>::pointer_to(__node_->__value_);
257 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000258
Howard Hinnant99acc502010-09-21 17:32:39 +0000259 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000260 __hash_iterator& operator++()
261 {
Howard Hinnant39213642013-07-23 22:01:58 +0000262#if _LIBCPP_DEBUG_LEVEL >= 2
263 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
264 "Attempted to increment non-incrementable unordered container iterator");
265#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000266 __node_ = __node_->__next_;
267 return *this;
268 }
269
Howard Hinnant99acc502010-09-21 17:32:39 +0000270 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000271 __hash_iterator operator++(int)
272 {
273 __hash_iterator __t(*this);
274 ++(*this);
275 return __t;
276 }
277
Howard Hinnant99acc502010-09-21 17:32:39 +0000278 friend _LIBCPP_INLINE_VISIBILITY
279 bool operator==(const __hash_iterator& __x, const __hash_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58 +0000280 {
Howard Hinnant39213642013-07-23 22:01:58 +0000281 return __x.__node_ == __y.__node_;
282 }
Howard Hinnant99acc502010-09-21 17:32:39 +0000283 friend _LIBCPP_INLINE_VISIBILITY
284 bool operator!=(const __hash_iterator& __x, const __hash_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58 +0000285 {return !(__x == __y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000286
287private:
Howard Hinnant39213642013-07-23 22:01:58 +0000288#if _LIBCPP_DEBUG_LEVEL >= 2
289 _LIBCPP_INLINE_VISIBILITY
290 __hash_iterator(__node_pointer __node, const void* __c) _NOEXCEPT
291 : __node_(__node)
292 {
293 __get_db()->__insert_ic(this, __c);
294 }
295#else
Howard Hinnant99acc502010-09-21 17:32:39 +0000296 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000297 __hash_iterator(__node_pointer __node) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000298 : __node_(__node)
299 {}
Howard Hinnant39213642013-07-23 22:01:58 +0000300#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000301
302 template <class, class, class, class> friend class __hash_table;
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000303 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_const_iterator;
304 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_map_iterator;
305 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_map;
306 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_multimap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000307};
308
Eric Fiselier774c7c52016-02-10 20:46:23 +0000309template <class _NodePtr>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000310class _LIBCPP_TYPE_VIS_ONLY __hash_const_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000311{
Eric Fiselier774c7c52016-02-10 20:46:23 +0000312 static_assert(!is_const<typename pointer_traits<_NodePtr>::element_type>::value, "");
313 typedef __hash_node_types<_NodePtr> _NodeTypes;
314 typedef _NodePtr __node_pointer;
315 typedef __hash_iterator<_NodePtr> __non_const_iterator;
316 __node_pointer __node_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000317
318public:
Eric Fiselier774c7c52016-02-10 20:46:23 +0000319 typedef forward_iterator_tag iterator_category;
320 typedef typename _NodeTypes::__node_value_type value_type;
321 typedef typename _NodeTypes::difference_type difference_type;
322 typedef const value_type& reference;
323 typedef typename _NodeTypes::__const_node_value_type_pointer pointer;
324
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000325
Howard Hinnant39213642013-07-23 22:01:58 +0000326 _LIBCPP_INLINE_VISIBILITY __hash_const_iterator() _NOEXCEPT
Marshall Clow193ef032013-08-07 21:30:44 +0000327#if _LIBCPP_STD_VER > 11
328 : __node_(nullptr)
329#endif
Howard Hinnant39213642013-07-23 22:01:58 +0000330 {
331#if _LIBCPP_DEBUG_LEVEL >= 2
332 __get_db()->__insert_i(this);
333#endif
334 }
Howard Hinnant99acc502010-09-21 17:32:39 +0000335 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000336 __hash_const_iterator(const __non_const_iterator& __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000337 : __node_(__x.__node_)
Howard Hinnant39213642013-07-23 22:01:58 +0000338 {
339#if _LIBCPP_DEBUG_LEVEL >= 2
340 __get_db()->__iterator_copy(this, &__x);
341#endif
342 }
343
344#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000345
Howard Hinnant99acc502010-09-21 17:32:39 +0000346 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000347 __hash_const_iterator(const __hash_const_iterator& __i)
348 : __node_(__i.__node_)
349 {
350 __get_db()->__iterator_copy(this, &__i);
351 }
352
Howard Hinnant99acc502010-09-21 17:32:39 +0000353 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000354 ~__hash_const_iterator()
355 {
356 __get_db()->__erase_i(this);
357 }
358
359 _LIBCPP_INLINE_VISIBILITY
360 __hash_const_iterator& operator=(const __hash_const_iterator& __i)
361 {
362 if (this != &__i)
363 {
364 __get_db()->__iterator_copy(this, &__i);
365 __node_ = __i.__node_;
366 }
367 return *this;
368 }
369
370#endif // _LIBCPP_DEBUG_LEVEL >= 2
371
372 _LIBCPP_INLINE_VISIBILITY
373 reference operator*() const
374 {
375#if _LIBCPP_DEBUG_LEVEL >= 2
376 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
377 "Attempted to dereference a non-dereferenceable unordered container const_iterator");
378#endif
379 return __node_->__value_;
380 }
381 _LIBCPP_INLINE_VISIBILITY
382 pointer operator->() const
383 {
384#if _LIBCPP_DEBUG_LEVEL >= 2
385 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
386 "Attempted to dereference a non-dereferenceable unordered container const_iterator");
387#endif
388 return pointer_traits<pointer>::pointer_to(__node_->__value_);
389 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000390
Howard Hinnant99acc502010-09-21 17:32:39 +0000391 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000392 __hash_const_iterator& operator++()
393 {
Howard Hinnant39213642013-07-23 22:01:58 +0000394#if _LIBCPP_DEBUG_LEVEL >= 2
395 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
396 "Attempted to increment non-incrementable unordered container const_iterator");
397#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000398 __node_ = __node_->__next_;
399 return *this;
400 }
401
Howard Hinnant99acc502010-09-21 17:32:39 +0000402 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000403 __hash_const_iterator operator++(int)
404 {
405 __hash_const_iterator __t(*this);
406 ++(*this);
407 return __t;
408 }
409
Howard Hinnant99acc502010-09-21 17:32:39 +0000410 friend _LIBCPP_INLINE_VISIBILITY
411 bool operator==(const __hash_const_iterator& __x, const __hash_const_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58 +0000412 {
Howard Hinnant39213642013-07-23 22:01:58 +0000413 return __x.__node_ == __y.__node_;
414 }
Howard Hinnant99acc502010-09-21 17:32:39 +0000415 friend _LIBCPP_INLINE_VISIBILITY
416 bool operator!=(const __hash_const_iterator& __x, const __hash_const_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58 +0000417 {return !(__x == __y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000418
419private:
Howard Hinnant39213642013-07-23 22:01:58 +0000420#if _LIBCPP_DEBUG_LEVEL >= 2
421 _LIBCPP_INLINE_VISIBILITY
422 __hash_const_iterator(__node_pointer __node, const void* __c) _NOEXCEPT
423 : __node_(__node)
424 {
425 __get_db()->__insert_ic(this, __c);
426 }
427#else
Howard Hinnant99acc502010-09-21 17:32:39 +0000428 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000429 __hash_const_iterator(__node_pointer __node) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000430 : __node_(__node)
431 {}
Howard Hinnant39213642013-07-23 22:01:58 +0000432#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000433
434 template <class, class, class, class> friend class __hash_table;
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000435 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_map_const_iterator;
436 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_map;
437 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_multimap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000438};
439
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000440template <class _NodePtr>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000441class _LIBCPP_TYPE_VIS_ONLY __hash_local_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000442{
Eric Fiselier774c7c52016-02-10 20:46:23 +0000443 typedef __hash_node_types<_NodePtr> _NodeTypes;
444 typedef _NodePtr __node_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000445
446 __node_pointer __node_;
447 size_t __bucket_;
448 size_t __bucket_count_;
449
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000450public:
451 typedef forward_iterator_tag iterator_category;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000452 typedef typename _NodeTypes::__node_value_type value_type;
453 typedef typename _NodeTypes::difference_type difference_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000454 typedef value_type& reference;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000455 typedef typename _NodeTypes::__node_value_type_pointer pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000456
Howard Hinnant39213642013-07-23 22:01:58 +0000457 _LIBCPP_INLINE_VISIBILITY __hash_local_iterator() _NOEXCEPT
458 {
459#if _LIBCPP_DEBUG_LEVEL >= 2
460 __get_db()->__insert_i(this);
461#endif
462 }
463
464#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000465
Howard Hinnant99acc502010-09-21 17:32:39 +0000466 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000467 __hash_local_iterator(const __hash_local_iterator& __i)
468 : __node_(__i.__node_),
469 __bucket_(__i.__bucket_),
470 __bucket_count_(__i.__bucket_count_)
471 {
472 __get_db()->__iterator_copy(this, &__i);
473 }
474
Howard Hinnant99acc502010-09-21 17:32:39 +0000475 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000476 ~__hash_local_iterator()
477 {
478 __get_db()->__erase_i(this);
479 }
480
481 _LIBCPP_INLINE_VISIBILITY
482 __hash_local_iterator& operator=(const __hash_local_iterator& __i)
483 {
484 if (this != &__i)
485 {
486 __get_db()->__iterator_copy(this, &__i);
487 __node_ = __i.__node_;
488 __bucket_ = __i.__bucket_;
489 __bucket_count_ = __i.__bucket_count_;
490 }
491 return *this;
492 }
493
494#endif // _LIBCPP_DEBUG_LEVEL >= 2
495
496 _LIBCPP_INLINE_VISIBILITY
497 reference operator*() const
498 {
499#if _LIBCPP_DEBUG_LEVEL >= 2
500 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
501 "Attempted to dereference a non-dereferenceable unordered container local_iterator");
502#endif
503 return __node_->__value_;
504 }
505 _LIBCPP_INLINE_VISIBILITY
506 pointer operator->() const
507 {
508#if _LIBCPP_DEBUG_LEVEL >= 2
509 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
510 "Attempted to dereference a non-dereferenceable unordered container local_iterator");
511#endif
512 return pointer_traits<pointer>::pointer_to(__node_->__value_);
513 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000514
Howard Hinnant99acc502010-09-21 17:32:39 +0000515 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000516 __hash_local_iterator& operator++()
517 {
Howard Hinnant39213642013-07-23 22:01:58 +0000518#if _LIBCPP_DEBUG_LEVEL >= 2
519 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
520 "Attempted to increment non-incrementable unordered container local_iterator");
521#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000522 __node_ = __node_->__next_;
Howard Hinnant7a445152012-07-06 17:31:14 +0000523 if (__node_ != nullptr && __constrain_hash(__node_->__hash_, __bucket_count_) != __bucket_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000524 __node_ = nullptr;
525 return *this;
526 }
527
Howard Hinnant99acc502010-09-21 17:32:39 +0000528 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000529 __hash_local_iterator operator++(int)
530 {
531 __hash_local_iterator __t(*this);
532 ++(*this);
533 return __t;
534 }
535
Howard Hinnant99acc502010-09-21 17:32:39 +0000536 friend _LIBCPP_INLINE_VISIBILITY
537 bool operator==(const __hash_local_iterator& __x, const __hash_local_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58 +0000538 {
Howard Hinnant39213642013-07-23 22:01:58 +0000539 return __x.__node_ == __y.__node_;
540 }
Howard Hinnant99acc502010-09-21 17:32:39 +0000541 friend _LIBCPP_INLINE_VISIBILITY
542 bool operator!=(const __hash_local_iterator& __x, const __hash_local_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58 +0000543 {return !(__x == __y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000544
545private:
Howard Hinnant39213642013-07-23 22:01:58 +0000546#if _LIBCPP_DEBUG_LEVEL >= 2
547 _LIBCPP_INLINE_VISIBILITY
548 __hash_local_iterator(__node_pointer __node, size_t __bucket,
549 size_t __bucket_count, const void* __c) _NOEXCEPT
550 : __node_(__node),
551 __bucket_(__bucket),
552 __bucket_count_(__bucket_count)
553 {
554 __get_db()->__insert_ic(this, __c);
555 if (__node_ != nullptr)
556 __node_ = __node_->__next_;
557 }
558#else
Howard Hinnant99acc502010-09-21 17:32:39 +0000559 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000560 __hash_local_iterator(__node_pointer __node, size_t __bucket,
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000561 size_t __bucket_count) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000562 : __node_(__node),
563 __bucket_(__bucket),
564 __bucket_count_(__bucket_count)
565 {
566 if (__node_ != nullptr)
567 __node_ = __node_->__next_;
568 }
Howard Hinnant39213642013-07-23 22:01:58 +0000569#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000570 template <class, class, class, class> friend class __hash_table;
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000571 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_const_local_iterator;
572 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_map_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000573};
574
575template <class _ConstNodePtr>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000576class _LIBCPP_TYPE_VIS_ONLY __hash_const_local_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000577{
Eric Fiselier774c7c52016-02-10 20:46:23 +0000578 typedef __hash_node_types<_ConstNodePtr> _NodeTypes;
579 typedef _ConstNodePtr __node_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000580
581 __node_pointer __node_;
582 size_t __bucket_;
583 size_t __bucket_count_;
584
585 typedef pointer_traits<__node_pointer> __pointer_traits;
586 typedef typename __pointer_traits::element_type __node;
587 typedef typename remove_const<__node>::type __non_const_node;
Eric Fiselier5cf84e02015-12-30 21:52:00 +0000588 typedef typename __rebind_pointer<__node_pointer, __non_const_node>::type
589 __non_const_node_pointer;
590
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000591 typedef __hash_local_iterator<__non_const_node_pointer>
592 __non_const_iterator;
593public:
Eric Fiselier774c7c52016-02-10 20:46:23 +0000594 typedef forward_iterator_tag iterator_category;
595 typedef typename _NodeTypes::__node_value_type value_type;
596 typedef typename _NodeTypes::difference_type difference_type;
597 typedef const value_type& reference;
598 typedef typename _NodeTypes::__const_node_value_type_pointer pointer;
Eric Fiselier5cf84e02015-12-30 21:52:00 +0000599
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000600
Howard Hinnant39213642013-07-23 22:01:58 +0000601 _LIBCPP_INLINE_VISIBILITY __hash_const_local_iterator() _NOEXCEPT
602 {
603#if _LIBCPP_DEBUG_LEVEL >= 2
604 __get_db()->__insert_i(this);
605#endif
606 }
607
Howard Hinnant99acc502010-09-21 17:32:39 +0000608 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000609 __hash_const_local_iterator(const __non_const_iterator& __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000610 : __node_(__x.__node_),
611 __bucket_(__x.__bucket_),
612 __bucket_count_(__x.__bucket_count_)
Howard Hinnant39213642013-07-23 22:01:58 +0000613 {
614#if _LIBCPP_DEBUG_LEVEL >= 2
615 __get_db()->__iterator_copy(this, &__x);
616#endif
617 }
618
619#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000620
Howard Hinnant99acc502010-09-21 17:32:39 +0000621 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000622 __hash_const_local_iterator(const __hash_const_local_iterator& __i)
623 : __node_(__i.__node_),
624 __bucket_(__i.__bucket_),
625 __bucket_count_(__i.__bucket_count_)
626 {
627 __get_db()->__iterator_copy(this, &__i);
628 }
629
Howard Hinnant99acc502010-09-21 17:32:39 +0000630 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000631 ~__hash_const_local_iterator()
632 {
633 __get_db()->__erase_i(this);
634 }
635
636 _LIBCPP_INLINE_VISIBILITY
637 __hash_const_local_iterator& operator=(const __hash_const_local_iterator& __i)
638 {
639 if (this != &__i)
640 {
641 __get_db()->__iterator_copy(this, &__i);
642 __node_ = __i.__node_;
643 __bucket_ = __i.__bucket_;
644 __bucket_count_ = __i.__bucket_count_;
645 }
646 return *this;
647 }
648
649#endif // _LIBCPP_DEBUG_LEVEL >= 2
650
651 _LIBCPP_INLINE_VISIBILITY
652 reference operator*() const
653 {
654#if _LIBCPP_DEBUG_LEVEL >= 2
655 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
656 "Attempted to dereference a non-dereferenceable unordered container const_local_iterator");
657#endif
658 return __node_->__value_;
659 }
660 _LIBCPP_INLINE_VISIBILITY
661 pointer operator->() const
662 {
663#if _LIBCPP_DEBUG_LEVEL >= 2
664 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
665 "Attempted to dereference a non-dereferenceable unordered container const_local_iterator");
666#endif
667 return pointer_traits<pointer>::pointer_to(__node_->__value_);
668 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000669
Howard Hinnant99acc502010-09-21 17:32:39 +0000670 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000671 __hash_const_local_iterator& operator++()
672 {
Howard Hinnant39213642013-07-23 22:01:58 +0000673#if _LIBCPP_DEBUG_LEVEL >= 2
674 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
675 "Attempted to increment non-incrementable unordered container const_local_iterator");
676#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000677 __node_ = __node_->__next_;
Howard Hinnant7a445152012-07-06 17:31:14 +0000678 if (__node_ != nullptr && __constrain_hash(__node_->__hash_, __bucket_count_) != __bucket_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000679 __node_ = nullptr;
680 return *this;
681 }
682
Howard Hinnant99acc502010-09-21 17:32:39 +0000683 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000684 __hash_const_local_iterator operator++(int)
685 {
686 __hash_const_local_iterator __t(*this);
687 ++(*this);
688 return __t;
689 }
690
Howard Hinnant99acc502010-09-21 17:32:39 +0000691 friend _LIBCPP_INLINE_VISIBILITY
692 bool operator==(const __hash_const_local_iterator& __x, const __hash_const_local_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58 +0000693 {
Howard Hinnant39213642013-07-23 22:01:58 +0000694 return __x.__node_ == __y.__node_;
695 }
Howard Hinnant99acc502010-09-21 17:32:39 +0000696 friend _LIBCPP_INLINE_VISIBILITY
697 bool operator!=(const __hash_const_local_iterator& __x, const __hash_const_local_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58 +0000698 {return !(__x == __y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000699
700private:
Howard Hinnant39213642013-07-23 22:01:58 +0000701#if _LIBCPP_DEBUG_LEVEL >= 2
702 _LIBCPP_INLINE_VISIBILITY
703 __hash_const_local_iterator(__node_pointer __node, size_t __bucket,
704 size_t __bucket_count, const void* __c) _NOEXCEPT
705 : __node_(__node),
706 __bucket_(__bucket),
707 __bucket_count_(__bucket_count)
708 {
709 __get_db()->__insert_ic(this, __c);
710 if (__node_ != nullptr)
711 __node_ = __node_->__next_;
712 }
713#else
Howard Hinnant99acc502010-09-21 17:32:39 +0000714 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000715 __hash_const_local_iterator(__node_pointer __node, size_t __bucket,
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000716 size_t __bucket_count) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000717 : __node_(__node),
718 __bucket_(__bucket),
719 __bucket_count_(__bucket_count)
720 {
721 if (__node_ != nullptr)
722 __node_ = __node_->__next_;
723 }
Howard Hinnant39213642013-07-23 22:01:58 +0000724#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000725 template <class, class, class, class> friend class __hash_table;
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000726 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_map_const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000727};
728
729template <class _Alloc>
730class __bucket_list_deallocator
731{
732 typedef _Alloc allocator_type;
733 typedef allocator_traits<allocator_type> __alloc_traits;
734 typedef typename __alloc_traits::size_type size_type;
735
736 __compressed_pair<size_type, allocator_type> __data_;
737public:
738 typedef typename __alloc_traits::pointer pointer;
739
Howard Hinnant99acc502010-09-21 17:32:39 +0000740 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000741 __bucket_list_deallocator()
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000742 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000743 : __data_(0) {}
Howard Hinnant99acc502010-09-21 17:32:39 +0000744
745 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000746 __bucket_list_deallocator(const allocator_type& __a, size_type __size)
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000747 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000748 : __data_(__size, __a) {}
749
Howard Hinnant73d21a42010-09-04 23:28:19 +0000750#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000751
Howard Hinnant99acc502010-09-21 17:32:39 +0000752 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000753 __bucket_list_deallocator(__bucket_list_deallocator&& __x)
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000754 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000755 : __data_(_VSTD::move(__x.__data_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000756 {
757 __x.size() = 0;
758 }
759
Howard Hinnant73d21a42010-09-04 23:28:19 +0000760#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000761
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000762 _LIBCPP_INLINE_VISIBILITY
763 size_type& size() _NOEXCEPT {return __data_.first();}
764 _LIBCPP_INLINE_VISIBILITY
765 size_type size() const _NOEXCEPT {return __data_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000766
Howard Hinnant99acc502010-09-21 17:32:39 +0000767 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000768 allocator_type& __alloc() _NOEXCEPT {return __data_.second();}
769 _LIBCPP_INLINE_VISIBILITY
770 const allocator_type& __alloc() const _NOEXCEPT {return __data_.second();}
771
772 _LIBCPP_INLINE_VISIBILITY
773 void operator()(pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000774 {
775 __alloc_traits::deallocate(__alloc(), __p, size());
776 }
777};
778
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000779template <class _Alloc> class __hash_map_node_destructor;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000780
781template <class _Alloc>
782class __hash_node_destructor
783{
784 typedef _Alloc allocator_type;
785 typedef allocator_traits<allocator_type> __alloc_traits;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000786
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000787public:
788 typedef typename __alloc_traits::pointer pointer;
789private:
790
791 allocator_type& __na_;
792
793 __hash_node_destructor& operator=(const __hash_node_destructor&);
794
795public:
796 bool __value_constructed;
797
Howard Hinnant99acc502010-09-21 17:32:39 +0000798 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant199d0ae2011-07-31 17:04:30 +0000799 explicit __hash_node_destructor(allocator_type& __na,
800 bool __constructed = false) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000801 : __na_(__na),
Howard Hinnant199d0ae2011-07-31 17:04:30 +0000802 __value_constructed(__constructed)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000803 {}
804
Howard Hinnant99acc502010-09-21 17:32:39 +0000805 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000806 void operator()(pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000807 {
808 if (__value_constructed)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000809 __alloc_traits::destroy(__na_, _VSTD::addressof(__p->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000810 if (__p)
811 __alloc_traits::deallocate(__na_, __p, 1);
812 }
813
814 template <class> friend class __hash_map_node_destructor;
815};
816
817template <class _Tp, class _Hash, class _Equal, class _Alloc>
818class __hash_table
819{
820public:
821 typedef _Tp value_type;
822 typedef _Hash hasher;
823 typedef _Equal key_equal;
824 typedef _Alloc allocator_type;
825
826private:
827 typedef allocator_traits<allocator_type> __alloc_traits;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000828 typedef typename
829 __make_hash_node_types<value_type, typename __alloc_traits::void_pointer>::type
830 _NodeTypes;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000831public:
832 typedef value_type& reference;
833 typedef const value_type& const_reference;
834 typedef typename __alloc_traits::pointer pointer;
835 typedef typename __alloc_traits::const_pointer const_pointer;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000836#ifndef _LIBCPP_ABI_FIX_UNORDERED_CONTAINER_SIZE_TYPE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000837 typedef typename __alloc_traits::size_type size_type;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000838#else
839 typedef typename _NodeTypes::size_type size_type;
840#endif
841 typedef typename _NodeTypes::difference_type difference_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000842public:
843 // Create __node
Eric Fiselier774c7c52016-02-10 20:46:23 +0000844
845 typedef typename _NodeTypes::__node_type __node;
Marshall Clow66302c62015-04-07 05:21:38 +0000846 typedef typename __rebind_alloc_helper<__alloc_traits, __node>::type __node_allocator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000847 typedef allocator_traits<__node_allocator> __node_traits;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000848 typedef typename _NodeTypes::__node_pointer __node_pointer;
849 typedef typename _NodeTypes::__node_pointer __node_const_pointer;
850 typedef typename _NodeTypes::__node_base_type __first_node;
851 typedef typename _NodeTypes::__node_base_pointer __node_base_pointer;
852
853private:
854 // check for sane allocator pointer rebinding semantics. Rebinding the
855 // allocator for a new pointer type should be exactly the same as rebinding
856 // the pointer using 'pointer_traits'.
857 static_assert((is_same<__node_pointer, typename __node_traits::pointer>::value),
858 "Allocator does not rebind pointers in a sane manner.");
859 typedef typename __rebind_alloc_helper<__node_traits, __first_node>::type
860 __node_base_allocator;
861 typedef allocator_traits<__node_base_allocator> __node_base_traits;
862 static_assert((is_same<__node_base_pointer, typename __node_base_traits::pointer>::value),
863 "Allocator does not rebind pointers in a sane manner.");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000864
865private:
866
Marshall Clow66302c62015-04-07 05:21:38 +0000867 typedef typename __rebind_alloc_helper<__node_traits, __node_pointer>::type __pointer_allocator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000868 typedef __bucket_list_deallocator<__pointer_allocator> __bucket_list_deleter;
869 typedef unique_ptr<__node_pointer[], __bucket_list_deleter> __bucket_list;
870 typedef allocator_traits<__pointer_allocator> __pointer_alloc_traits;
871 typedef typename __bucket_list_deleter::pointer __node_pointer_pointer;
872
873 // --- Member data begin ---
Eric Fiselier774c7c52016-02-10 20:46:23 +0000874 __bucket_list __bucket_list_;
875 __compressed_pair<__first_node, __node_allocator> __p1_;
876 __compressed_pair<size_type, hasher> __p2_;
877 __compressed_pair<float, key_equal> __p3_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000878 // --- Member data end ---
879
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000880 _LIBCPP_INLINE_VISIBILITY
881 size_type& size() _NOEXCEPT {return __p2_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000882public:
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000883 _LIBCPP_INLINE_VISIBILITY
884 size_type size() const _NOEXCEPT {return __p2_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000885
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000886 _LIBCPP_INLINE_VISIBILITY
887 hasher& hash_function() _NOEXCEPT {return __p2_.second();}
888 _LIBCPP_INLINE_VISIBILITY
889 const hasher& hash_function() const _NOEXCEPT {return __p2_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000890
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000891 _LIBCPP_INLINE_VISIBILITY
892 float& max_load_factor() _NOEXCEPT {return __p3_.first();}
893 _LIBCPP_INLINE_VISIBILITY
894 float max_load_factor() const _NOEXCEPT {return __p3_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000895
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000896 _LIBCPP_INLINE_VISIBILITY
897 key_equal& key_eq() _NOEXCEPT {return __p3_.second();}
898 _LIBCPP_INLINE_VISIBILITY
899 const key_equal& key_eq() const _NOEXCEPT {return __p3_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000900
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000901 _LIBCPP_INLINE_VISIBILITY
902 __node_allocator& __node_alloc() _NOEXCEPT {return __p1_.second();}
903 _LIBCPP_INLINE_VISIBILITY
904 const __node_allocator& __node_alloc() const _NOEXCEPT
905 {return __p1_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000906
907public:
908 typedef __hash_iterator<__node_pointer> iterator;
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +0000909 typedef __hash_const_iterator<__node_pointer> const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000910 typedef __hash_local_iterator<__node_pointer> local_iterator;
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +0000911 typedef __hash_const_local_iterator<__node_pointer> const_local_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000912
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000913 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000914 __hash_table()
915 _NOEXCEPT_(
916 is_nothrow_default_constructible<__bucket_list>::value &&
917 is_nothrow_default_constructible<__first_node>::value &&
918 is_nothrow_default_constructible<__node_allocator>::value &&
919 is_nothrow_default_constructible<hasher>::value &&
920 is_nothrow_default_constructible<key_equal>::value);
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000921 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000922 __hash_table(const hasher& __hf, const key_equal& __eql);
923 __hash_table(const hasher& __hf, const key_equal& __eql,
924 const allocator_type& __a);
925 explicit __hash_table(const allocator_type& __a);
926 __hash_table(const __hash_table& __u);
927 __hash_table(const __hash_table& __u, const allocator_type& __a);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000928#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000929 __hash_table(__hash_table&& __u)
930 _NOEXCEPT_(
931 is_nothrow_move_constructible<__bucket_list>::value &&
932 is_nothrow_move_constructible<__first_node>::value &&
933 is_nothrow_move_constructible<__node_allocator>::value &&
934 is_nothrow_move_constructible<hasher>::value &&
935 is_nothrow_move_constructible<key_equal>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000936 __hash_table(__hash_table&& __u, const allocator_type& __a);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000937#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000938 ~__hash_table();
939
940 __hash_table& operator=(const __hash_table& __u);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000941#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +0000942 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000943 __hash_table& operator=(__hash_table&& __u)
944 _NOEXCEPT_(
945 __node_traits::propagate_on_container_move_assignment::value &&
946 is_nothrow_move_assignable<__node_allocator>::value &&
947 is_nothrow_move_assignable<hasher>::value &&
948 is_nothrow_move_assignable<key_equal>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000949#endif
950 template <class _InputIterator>
951 void __assign_unique(_InputIterator __first, _InputIterator __last);
952 template <class _InputIterator>
953 void __assign_multi(_InputIterator __first, _InputIterator __last);
954
Howard Hinnant99acc502010-09-21 17:32:39 +0000955 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000956 size_type max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000957 {
958 return allocator_traits<__pointer_allocator>::max_size(
959 __bucket_list_.get_deleter().__alloc());
960 }
961
962 pair<iterator, bool> __node_insert_unique(__node_pointer __nd);
963 iterator __node_insert_multi(__node_pointer __nd);
964 iterator __node_insert_multi(const_iterator __p,
965 __node_pointer __nd);
966
Howard Hinnant73d21a42010-09-04 23:28:19 +0000967#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000968 template <class... _Args>
969 pair<iterator, bool> __emplace_unique(_Args&&... __args);
970 template <class... _Args>
971 iterator __emplace_multi(_Args&&... __args);
972 template <class... _Args>
973 iterator __emplace_hint_multi(const_iterator __p, _Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000974#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000975
Eric Fiselierfdae69a2015-06-13 07:18:32 +0000976#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
977 template <class _ValueTp>
978 _LIBCPP_INLINE_VISIBILITY
979 pair<iterator, bool> __insert_unique_value(_ValueTp&& __x);
980#else
981 _LIBCPP_INLINE_VISIBILITY
982 pair<iterator, bool> __insert_unique_value(const value_type& __x);
983#endif
984
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000985 pair<iterator, bool> __insert_unique(const value_type& __x);
986
Howard Hinnant73d21a42010-09-04 23:28:19 +0000987#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Eric Fiselierfdae69a2015-06-13 07:18:32 +0000988 pair<iterator, bool> __insert_unique(value_type&& __x);
Howard Hinnant99968442011-11-29 18:15:50 +0000989 template <class _Pp>
Eric Fiselierfdae69a2015-06-13 07:18:32 +0000990 pair<iterator, bool> __insert_unique(_Pp&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000991#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000992
Howard Hinnant73d21a42010-09-04 23:28:19 +0000993#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50 +0000994 template <class _Pp>
995 iterator __insert_multi(_Pp&& __x);
996 template <class _Pp>
997 iterator __insert_multi(const_iterator __p, _Pp&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000998#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000999 iterator __insert_multi(const value_type& __x);
1000 iterator __insert_multi(const_iterator __p, const value_type& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001001#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001002
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001003 void clear() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001004 void rehash(size_type __n);
Howard Hinnant99acc502010-09-21 17:32:39 +00001005 _LIBCPP_INLINE_VISIBILITY void reserve(size_type __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001006 {rehash(static_cast<size_type>(ceil(__n / max_load_factor())));}
Howard Hinnant99acc502010-09-21 17:32:39 +00001007
1008 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001009 size_type bucket_count() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001010 {
1011 return __bucket_list_.get_deleter().size();
1012 }
1013
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001014 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001015 iterator begin() _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001016 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001017 iterator end() _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001018 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001019 const_iterator begin() const _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001020 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001021 const_iterator end() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001022
1023 template <class _Key>
Howard Hinnant99acc502010-09-21 17:32:39 +00001024 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001025 size_type bucket(const _Key& __k) const
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00001026 {
1027 _LIBCPP_ASSERT(bucket_count() > 0,
1028 "unordered container::bucket(key) called when bucket_count() == 0");
1029 return __constrain_hash(hash_function()(__k), bucket_count());
1030 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001031
1032 template <class _Key>
1033 iterator find(const _Key& __x);
1034 template <class _Key>
1035 const_iterator find(const _Key& __x) const;
1036
Howard Hinnant99968442011-11-29 18:15:50 +00001037 typedef __hash_node_destructor<__node_allocator> _Dp;
1038 typedef unique_ptr<__node, _Dp> __node_holder;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001039
1040 iterator erase(const_iterator __p);
1041 iterator erase(const_iterator __first, const_iterator __last);
1042 template <class _Key>
1043 size_type __erase_unique(const _Key& __k);
1044 template <class _Key>
1045 size_type __erase_multi(const _Key& __k);
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001046 __node_holder remove(const_iterator __p) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001047
1048 template <class _Key>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001049 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001050 size_type __count_unique(const _Key& __k) const;
1051 template <class _Key>
1052 size_type __count_multi(const _Key& __k) const;
1053
1054 template <class _Key>
1055 pair<iterator, iterator>
1056 __equal_range_unique(const _Key& __k);
1057 template <class _Key>
1058 pair<const_iterator, const_iterator>
1059 __equal_range_unique(const _Key& __k) const;
1060
1061 template <class _Key>
1062 pair<iterator, iterator>
1063 __equal_range_multi(const _Key& __k);
1064 template <class _Key>
1065 pair<const_iterator, const_iterator>
1066 __equal_range_multi(const _Key& __k) const;
1067
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001068 void swap(__hash_table& __u)
Eric Fiselier692177d2015-07-18 20:40:46 +00001069#if _LIBCPP_STD_VER <= 11
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001070 _NOEXCEPT_(
Marshall Clow7d914d12015-07-13 20:04:56 +00001071 __is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value
Marshall Clow7d914d12015-07-13 20:04:56 +00001072 && (!allocator_traits<__pointer_allocator>::propagate_on_container_swap::value
1073 || __is_nothrow_swappable<__pointer_allocator>::value)
1074 && (!__node_traits::propagate_on_container_swap::value
1075 || __is_nothrow_swappable<__node_allocator>::value)
Marshall Clow7d914d12015-07-13 20:04:56 +00001076 );
Eric Fiselier692177d2015-07-18 20:40:46 +00001077#else
1078 _NOEXCEPT_(__is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value);
1079#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001080
Howard Hinnant99acc502010-09-21 17:32:39 +00001081 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001082 size_type max_bucket_count() const _NOEXCEPT
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001083 {return __pointer_alloc_traits::max_size(__bucket_list_.get_deleter().__alloc());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001084 size_type bucket_size(size_type __n) const;
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001085 _LIBCPP_INLINE_VISIBILITY float load_factor() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001086 {
1087 size_type __bc = bucket_count();
1088 return __bc != 0 ? (float)size() / __bc : 0.f;
1089 }
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001090 _LIBCPP_INLINE_VISIBILITY void max_load_factor(float __mlf) _NOEXCEPT
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00001091 {
1092 _LIBCPP_ASSERT(__mlf > 0,
1093 "unordered container::max_load_factor(lf) called with lf <= 0");
1094 max_load_factor() = _VSTD::max(__mlf, load_factor());
1095 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001096
Howard Hinnant39213642013-07-23 22:01:58 +00001097 _LIBCPP_INLINE_VISIBILITY
1098 local_iterator
1099 begin(size_type __n)
1100 {
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00001101 _LIBCPP_ASSERT(__n < bucket_count(),
1102 "unordered container::begin(n) called with n >= bucket_count()");
Howard Hinnant39213642013-07-23 22:01:58 +00001103#if _LIBCPP_DEBUG_LEVEL >= 2
1104 return local_iterator(__bucket_list_[__n], __n, bucket_count(), this);
1105#else
1106 return local_iterator(__bucket_list_[__n], __n, bucket_count());
1107#endif
1108 }
1109
1110 _LIBCPP_INLINE_VISIBILITY
1111 local_iterator
1112 end(size_type __n)
1113 {
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00001114 _LIBCPP_ASSERT(__n < bucket_count(),
1115 "unordered container::end(n) called with n >= bucket_count()");
Howard Hinnant39213642013-07-23 22:01:58 +00001116#if _LIBCPP_DEBUG_LEVEL >= 2
1117 return local_iterator(nullptr, __n, bucket_count(), this);
1118#else
1119 return local_iterator(nullptr, __n, bucket_count());
1120#endif
1121 }
1122
1123 _LIBCPP_INLINE_VISIBILITY
1124 const_local_iterator
1125 cbegin(size_type __n) const
1126 {
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00001127 _LIBCPP_ASSERT(__n < bucket_count(),
1128 "unordered container::cbegin(n) called with n >= bucket_count()");
Howard Hinnant39213642013-07-23 22:01:58 +00001129#if _LIBCPP_DEBUG_LEVEL >= 2
1130 return const_local_iterator(__bucket_list_[__n], __n, bucket_count(), this);
1131#else
1132 return const_local_iterator(__bucket_list_[__n], __n, bucket_count());
1133#endif
1134 }
1135
1136 _LIBCPP_INLINE_VISIBILITY
1137 const_local_iterator
1138 cend(size_type __n) const
1139 {
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00001140 _LIBCPP_ASSERT(__n < bucket_count(),
1141 "unordered container::cend(n) called with n >= bucket_count()");
Howard Hinnant39213642013-07-23 22:01:58 +00001142#if _LIBCPP_DEBUG_LEVEL >= 2
1143 return const_local_iterator(nullptr, __n, bucket_count(), this);
1144#else
1145 return const_local_iterator(nullptr, __n, bucket_count());
1146#endif
1147 }
1148
1149#if _LIBCPP_DEBUG_LEVEL >= 2
1150
1151 bool __dereferenceable(const const_iterator* __i) const;
1152 bool __decrementable(const const_iterator* __i) const;
1153 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1154 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1155
1156#endif // _LIBCPP_DEBUG_LEVEL >= 2
1157
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001158private:
1159 void __rehash(size_type __n);
1160
Howard Hinnant73d21a42010-09-04 23:28:19 +00001161#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1162#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001163 template <class ..._Args>
1164 __node_holder __construct_node(_Args&& ...__args);
Howard Hinnantbfd55302010-09-04 23:46:48 +00001165#endif // _LIBCPP_HAS_NO_VARIADICS
Duncan P. N. Exon Smith95722352016-01-22 18:27:26 +00001166 template <class _ValueTp>
1167 __node_holder __construct_node_hash(_ValueTp&& __v, size_t __hash);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001168#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001169 __node_holder __construct_node(const value_type& __v);
1170#endif
Duncan P. N. Exon Smith95722352016-01-22 18:27:26 +00001171 __node_holder __construct_node_hash(const value_type& __v, size_t __hash);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001172
Howard Hinnant99acc502010-09-21 17:32:39 +00001173 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001174 void __copy_assign_alloc(const __hash_table& __u)
1175 {__copy_assign_alloc(__u, integral_constant<bool,
1176 __node_traits::propagate_on_container_copy_assignment::value>());}
1177 void __copy_assign_alloc(const __hash_table& __u, true_type);
Howard Hinnant99acc502010-09-21 17:32:39 +00001178 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00001179 void __copy_assign_alloc(const __hash_table&, false_type) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001180
1181 void __move_assign(__hash_table& __u, false_type);
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001182 void __move_assign(__hash_table& __u, true_type)
1183 _NOEXCEPT_(
1184 is_nothrow_move_assignable<__node_allocator>::value &&
1185 is_nothrow_move_assignable<hasher>::value &&
1186 is_nothrow_move_assignable<key_equal>::value);
1187 _LIBCPP_INLINE_VISIBILITY
1188 void __move_assign_alloc(__hash_table& __u)
1189 _NOEXCEPT_(
1190 !__node_traits::propagate_on_container_move_assignment::value ||
1191 (is_nothrow_move_assignable<__pointer_allocator>::value &&
1192 is_nothrow_move_assignable<__node_allocator>::value))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001193 {__move_assign_alloc(__u, integral_constant<bool,
1194 __node_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnant99acc502010-09-21 17:32:39 +00001195 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001196 void __move_assign_alloc(__hash_table& __u, true_type)
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001197 _NOEXCEPT_(
1198 is_nothrow_move_assignable<__pointer_allocator>::value &&
1199 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001200 {
1201 __bucket_list_.get_deleter().__alloc() =
Howard Hinnant0949eed2011-06-30 21:18:19 +00001202 _VSTD::move(__u.__bucket_list_.get_deleter().__alloc());
1203 __node_alloc() = _VSTD::move(__u.__node_alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001204 }
Howard Hinnant99acc502010-09-21 17:32:39 +00001205 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001206 void __move_assign_alloc(__hash_table&, false_type) _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001207
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001208 void __deallocate(__node_pointer __np) _NOEXCEPT;
1209 __node_pointer __detach() _NOEXCEPT;
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001210
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001211 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_map;
1212 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_multimap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001213};
1214
1215template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001216inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001217__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table()
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001218 _NOEXCEPT_(
1219 is_nothrow_default_constructible<__bucket_list>::value &&
1220 is_nothrow_default_constructible<__first_node>::value &&
Eric Fiselierc8f54c22015-12-16 00:53:04 +00001221 is_nothrow_default_constructible<__node_allocator>::value &&
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001222 is_nothrow_default_constructible<hasher>::value &&
1223 is_nothrow_default_constructible<key_equal>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001224 : __p2_(0),
1225 __p3_(1.0f)
1226{
1227}
1228
1229template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001230inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001231__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const hasher& __hf,
1232 const key_equal& __eql)
1233 : __bucket_list_(nullptr, __bucket_list_deleter()),
1234 __p1_(),
1235 __p2_(0, __hf),
1236 __p3_(1.0f, __eql)
1237{
1238}
1239
1240template <class _Tp, class _Hash, class _Equal, class _Alloc>
1241__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const hasher& __hf,
1242 const key_equal& __eql,
1243 const allocator_type& __a)
1244 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1245 __p1_(__node_allocator(__a)),
1246 __p2_(0, __hf),
1247 __p3_(1.0f, __eql)
1248{
1249}
1250
1251template <class _Tp, class _Hash, class _Equal, class _Alloc>
1252__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const allocator_type& __a)
1253 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1254 __p1_(__node_allocator(__a)),
1255 __p2_(0),
1256 __p3_(1.0f)
1257{
1258}
1259
1260template <class _Tp, class _Hash, class _Equal, class _Alloc>
1261__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const __hash_table& __u)
1262 : __bucket_list_(nullptr,
1263 __bucket_list_deleter(allocator_traits<__pointer_allocator>::
1264 select_on_container_copy_construction(
1265 __u.__bucket_list_.get_deleter().__alloc()), 0)),
1266 __p1_(allocator_traits<__node_allocator>::
1267 select_on_container_copy_construction(__u.__node_alloc())),
1268 __p2_(0, __u.hash_function()),
1269 __p3_(__u.__p3_)
1270{
1271}
1272
1273template <class _Tp, class _Hash, class _Equal, class _Alloc>
1274__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const __hash_table& __u,
1275 const allocator_type& __a)
1276 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1277 __p1_(__node_allocator(__a)),
1278 __p2_(0, __u.hash_function()),
1279 __p3_(__u.__p3_)
1280{
1281}
1282
Howard Hinnant73d21a42010-09-04 23:28:19 +00001283#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001284
1285template <class _Tp, class _Hash, class _Equal, class _Alloc>
1286__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(__hash_table&& __u)
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001287 _NOEXCEPT_(
1288 is_nothrow_move_constructible<__bucket_list>::value &&
1289 is_nothrow_move_constructible<__first_node>::value &&
Eric Fiselierc8f54c22015-12-16 00:53:04 +00001290 is_nothrow_move_constructible<__node_allocator>::value &&
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001291 is_nothrow_move_constructible<hasher>::value &&
1292 is_nothrow_move_constructible<key_equal>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001293 : __bucket_list_(_VSTD::move(__u.__bucket_list_)),
1294 __p1_(_VSTD::move(__u.__p1_)),
1295 __p2_(_VSTD::move(__u.__p2_)),
1296 __p3_(_VSTD::move(__u.__p3_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001297{
1298 if (size() > 0)
1299 {
Howard Hinnant7a445152012-07-06 17:31:14 +00001300 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash_, bucket_count())] =
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001301 static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001302 __u.__p1_.first().__next_ = nullptr;
1303 __u.size() = 0;
1304 }
1305}
1306
1307template <class _Tp, class _Hash, class _Equal, class _Alloc>
1308__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(__hash_table&& __u,
1309 const allocator_type& __a)
1310 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1311 __p1_(__node_allocator(__a)),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001312 __p2_(0, _VSTD::move(__u.hash_function())),
1313 __p3_(_VSTD::move(__u.__p3_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001314{
1315 if (__a == allocator_type(__u.__node_alloc()))
1316 {
1317 __bucket_list_.reset(__u.__bucket_list_.release());
1318 __bucket_list_.get_deleter().size() = __u.__bucket_list_.get_deleter().size();
1319 __u.__bucket_list_.get_deleter().size() = 0;
1320 if (__u.size() > 0)
1321 {
1322 __p1_.first().__next_ = __u.__p1_.first().__next_;
1323 __u.__p1_.first().__next_ = nullptr;
Howard Hinnant7a445152012-07-06 17:31:14 +00001324 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash_, bucket_count())] =
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001325 static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001326 size() = __u.size();
1327 __u.size() = 0;
1328 }
1329 }
1330}
1331
Howard Hinnant73d21a42010-09-04 23:28:19 +00001332#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001333
1334template <class _Tp, class _Hash, class _Equal, class _Alloc>
1335__hash_table<_Tp, _Hash, _Equal, _Alloc>::~__hash_table()
1336{
1337 __deallocate(__p1_.first().__next_);
Howard Hinnant39213642013-07-23 22:01:58 +00001338#if _LIBCPP_DEBUG_LEVEL >= 2
1339 __get_db()->__erase_c(this);
1340#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001341}
1342
1343template <class _Tp, class _Hash, class _Equal, class _Alloc>
1344void
1345__hash_table<_Tp, _Hash, _Equal, _Alloc>::__copy_assign_alloc(
1346 const __hash_table& __u, true_type)
1347{
1348 if (__node_alloc() != __u.__node_alloc())
1349 {
1350 clear();
1351 __bucket_list_.reset();
1352 __bucket_list_.get_deleter().size() = 0;
1353 }
1354 __bucket_list_.get_deleter().__alloc() = __u.__bucket_list_.get_deleter().__alloc();
1355 __node_alloc() = __u.__node_alloc();
1356}
1357
1358template <class _Tp, class _Hash, class _Equal, class _Alloc>
1359__hash_table<_Tp, _Hash, _Equal, _Alloc>&
1360__hash_table<_Tp, _Hash, _Equal, _Alloc>::operator=(const __hash_table& __u)
1361{
1362 if (this != &__u)
1363 {
1364 __copy_assign_alloc(__u);
1365 hash_function() = __u.hash_function();
1366 key_eq() = __u.key_eq();
1367 max_load_factor() = __u.max_load_factor();
1368 __assign_multi(__u.begin(), __u.end());
1369 }
1370 return *this;
1371}
1372
1373template <class _Tp, class _Hash, class _Equal, class _Alloc>
1374void
1375__hash_table<_Tp, _Hash, _Equal, _Alloc>::__deallocate(__node_pointer __np)
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001376 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001377{
1378 __node_allocator& __na = __node_alloc();
1379 while (__np != nullptr)
1380 {
1381 __node_pointer __next = __np->__next_;
Howard Hinnant39213642013-07-23 22:01:58 +00001382#if _LIBCPP_DEBUG_LEVEL >= 2
1383 __c_node* __c = __get_db()->__find_c_and_lock(this);
1384 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1385 {
1386 --__p;
1387 iterator* __i = static_cast<iterator*>((*__p)->__i_);
1388 if (__i->__node_ == __np)
1389 {
1390 (*__p)->__c_ = nullptr;
1391 if (--__c->end_ != __p)
1392 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1393 }
1394 }
1395 __get_db()->unlock();
1396#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00001397 __node_traits::destroy(__na, _VSTD::addressof(__np->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001398 __node_traits::deallocate(__na, __np, 1);
1399 __np = __next;
1400 }
1401}
1402
1403template <class _Tp, class _Hash, class _Equal, class _Alloc>
1404typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_pointer
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001405__hash_table<_Tp, _Hash, _Equal, _Alloc>::__detach() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001406{
1407 size_type __bc = bucket_count();
1408 for (size_type __i = 0; __i < __bc; ++__i)
1409 __bucket_list_[__i] = nullptr;
1410 size() = 0;
1411 __node_pointer __cache = __p1_.first().__next_;
1412 __p1_.first().__next_ = nullptr;
1413 return __cache;
1414}
1415
Howard Hinnant73d21a42010-09-04 23:28:19 +00001416#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001417
1418template <class _Tp, class _Hash, class _Equal, class _Alloc>
1419void
1420__hash_table<_Tp, _Hash, _Equal, _Alloc>::__move_assign(
1421 __hash_table& __u, true_type)
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001422 _NOEXCEPT_(
1423 is_nothrow_move_assignable<__node_allocator>::value &&
1424 is_nothrow_move_assignable<hasher>::value &&
1425 is_nothrow_move_assignable<key_equal>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001426{
1427 clear();
1428 __bucket_list_.reset(__u.__bucket_list_.release());
1429 __bucket_list_.get_deleter().size() = __u.__bucket_list_.get_deleter().size();
1430 __u.__bucket_list_.get_deleter().size() = 0;
1431 __move_assign_alloc(__u);
1432 size() = __u.size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001433 hash_function() = _VSTD::move(__u.hash_function());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001434 max_load_factor() = __u.max_load_factor();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001435 key_eq() = _VSTD::move(__u.key_eq());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001436 __p1_.first().__next_ = __u.__p1_.first().__next_;
1437 if (size() > 0)
1438 {
Howard Hinnant7a445152012-07-06 17:31:14 +00001439 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash_, bucket_count())] =
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001440 static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001441 __u.__p1_.first().__next_ = nullptr;
1442 __u.size() = 0;
1443 }
Howard Hinnant39213642013-07-23 22:01:58 +00001444#if _LIBCPP_DEBUG_LEVEL >= 2
1445 __get_db()->swap(this, &__u);
1446#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001447}
1448
1449template <class _Tp, class _Hash, class _Equal, class _Alloc>
1450void
1451__hash_table<_Tp, _Hash, _Equal, _Alloc>::__move_assign(
1452 __hash_table& __u, false_type)
1453{
1454 if (__node_alloc() == __u.__node_alloc())
1455 __move_assign(__u, true_type());
1456 else
1457 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001458 hash_function() = _VSTD::move(__u.hash_function());
1459 key_eq() = _VSTD::move(__u.key_eq());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001460 max_load_factor() = __u.max_load_factor();
1461 if (bucket_count() != 0)
1462 {
1463 __node_pointer __cache = __detach();
1464#ifndef _LIBCPP_NO_EXCEPTIONS
1465 try
1466 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001467#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001468 const_iterator __i = __u.begin();
1469 while (__cache != nullptr && __u.size() != 0)
1470 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001471 __cache->__value_ = _VSTD::move(__u.remove(__i++)->__value_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001472 __node_pointer __next = __cache->__next_;
1473 __node_insert_multi(__cache);
1474 __cache = __next;
1475 }
1476#ifndef _LIBCPP_NO_EXCEPTIONS
1477 }
1478 catch (...)
1479 {
1480 __deallocate(__cache);
1481 throw;
1482 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001483#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001484 __deallocate(__cache);
1485 }
1486 const_iterator __i = __u.begin();
1487 while (__u.size() != 0)
1488 {
1489 __node_holder __h =
Howard Hinnant0949eed2011-06-30 21:18:19 +00001490 __construct_node(_VSTD::move(__u.remove(__i++)->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001491 __node_insert_multi(__h.get());
1492 __h.release();
1493 }
1494 }
1495}
1496
1497template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001498inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001499__hash_table<_Tp, _Hash, _Equal, _Alloc>&
1500__hash_table<_Tp, _Hash, _Equal, _Alloc>::operator=(__hash_table&& __u)
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001501 _NOEXCEPT_(
1502 __node_traits::propagate_on_container_move_assignment::value &&
1503 is_nothrow_move_assignable<__node_allocator>::value &&
1504 is_nothrow_move_assignable<hasher>::value &&
1505 is_nothrow_move_assignable<key_equal>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001506{
1507 __move_assign(__u, integral_constant<bool,
1508 __node_traits::propagate_on_container_move_assignment::value>());
1509 return *this;
1510}
1511
Howard Hinnant73d21a42010-09-04 23:28:19 +00001512#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001513
1514template <class _Tp, class _Hash, class _Equal, class _Alloc>
1515template <class _InputIterator>
1516void
1517__hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_unique(_InputIterator __first,
1518 _InputIterator __last)
1519{
1520 if (bucket_count() != 0)
1521 {
1522 __node_pointer __cache = __detach();
1523#ifndef _LIBCPP_NO_EXCEPTIONS
1524 try
1525 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001526#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001527 for (; __cache != nullptr && __first != __last; ++__first)
1528 {
1529 __cache->__value_ = *__first;
1530 __node_pointer __next = __cache->__next_;
1531 __node_insert_unique(__cache);
1532 __cache = __next;
1533 }
1534#ifndef _LIBCPP_NO_EXCEPTIONS
1535 }
1536 catch (...)
1537 {
1538 __deallocate(__cache);
1539 throw;
1540 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001541#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001542 __deallocate(__cache);
1543 }
1544 for (; __first != __last; ++__first)
1545 __insert_unique(*__first);
1546}
1547
1548template <class _Tp, class _Hash, class _Equal, class _Alloc>
1549template <class _InputIterator>
1550void
1551__hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_multi(_InputIterator __first,
1552 _InputIterator __last)
1553{
1554 if (bucket_count() != 0)
1555 {
1556 __node_pointer __cache = __detach();
1557#ifndef _LIBCPP_NO_EXCEPTIONS
1558 try
1559 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001560#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001561 for (; __cache != nullptr && __first != __last; ++__first)
1562 {
1563 __cache->__value_ = *__first;
1564 __node_pointer __next = __cache->__next_;
1565 __node_insert_multi(__cache);
1566 __cache = __next;
1567 }
1568#ifndef _LIBCPP_NO_EXCEPTIONS
1569 }
1570 catch (...)
1571 {
1572 __deallocate(__cache);
1573 throw;
1574 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001575#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001576 __deallocate(__cache);
1577 }
1578 for (; __first != __last; ++__first)
1579 __insert_multi(*__first);
1580}
1581
1582template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001583inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001584typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001585__hash_table<_Tp, _Hash, _Equal, _Alloc>::begin() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001586{
Howard Hinnant39213642013-07-23 22:01:58 +00001587#if _LIBCPP_DEBUG_LEVEL >= 2
1588 return iterator(__p1_.first().__next_, this);
1589#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001590 return iterator(__p1_.first().__next_);
Howard Hinnant39213642013-07-23 22:01:58 +00001591#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001592}
1593
1594template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001595inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001596typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001597__hash_table<_Tp, _Hash, _Equal, _Alloc>::end() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001598{
Howard Hinnant39213642013-07-23 22:01:58 +00001599#if _LIBCPP_DEBUG_LEVEL >= 2
1600 return iterator(nullptr, this);
1601#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001602 return iterator(nullptr);
Howard Hinnant39213642013-07-23 22:01:58 +00001603#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001604}
1605
1606template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001607inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001608typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001609__hash_table<_Tp, _Hash, _Equal, _Alloc>::begin() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001610{
Howard Hinnant39213642013-07-23 22:01:58 +00001611#if _LIBCPP_DEBUG_LEVEL >= 2
1612 return const_iterator(__p1_.first().__next_, this);
1613#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001614 return const_iterator(__p1_.first().__next_);
Howard Hinnant39213642013-07-23 22:01:58 +00001615#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001616}
1617
1618template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001619inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001620typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001621__hash_table<_Tp, _Hash, _Equal, _Alloc>::end() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001622{
Howard Hinnant39213642013-07-23 22:01:58 +00001623#if _LIBCPP_DEBUG_LEVEL >= 2
1624 return const_iterator(nullptr, this);
1625#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001626 return const_iterator(nullptr);
Howard Hinnant39213642013-07-23 22:01:58 +00001627#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001628}
1629
1630template <class _Tp, class _Hash, class _Equal, class _Alloc>
1631void
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001632__hash_table<_Tp, _Hash, _Equal, _Alloc>::clear() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001633{
1634 if (size() > 0)
1635 {
1636 __deallocate(__p1_.first().__next_);
1637 __p1_.first().__next_ = nullptr;
1638 size_type __bc = bucket_count();
Howard Hinnant9f66bff2011-07-05 14:14:17 +00001639 for (size_type __i = 0; __i < __bc; ++__i)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001640 __bucket_list_[__i] = nullptr;
1641 size() = 0;
1642 }
1643}
1644
1645template <class _Tp, class _Hash, class _Equal, class _Alloc>
1646pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
1647__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_unique(__node_pointer __nd)
1648{
1649 __nd->__hash_ = hash_function()(__nd->__value_);
1650 size_type __bc = bucket_count();
1651 bool __inserted = false;
1652 __node_pointer __ndptr;
1653 size_t __chash;
1654 if (__bc != 0)
1655 {
Howard Hinnant7a445152012-07-06 17:31:14 +00001656 __chash = __constrain_hash(__nd->__hash_, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001657 __ndptr = __bucket_list_[__chash];
1658 if (__ndptr != nullptr)
1659 {
1660 for (__ndptr = __ndptr->__next_; __ndptr != nullptr &&
Howard Hinnant7a445152012-07-06 17:31:14 +00001661 __constrain_hash(__ndptr->__hash_, __bc) == __chash;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001662 __ndptr = __ndptr->__next_)
1663 {
1664 if (key_eq()(__ndptr->__value_, __nd->__value_))
1665 goto __done;
1666 }
1667 }
1668 }
1669 {
1670 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1671 {
Eric Fiselier57947ca2015-02-02 21:31:48 +00001672 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001673 size_type(ceil(float(size() + 1) / max_load_factor()))));
1674 __bc = bucket_count();
Howard Hinnant7a445152012-07-06 17:31:14 +00001675 __chash = __constrain_hash(__nd->__hash_, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001676 }
1677 // insert_after __bucket_list_[__chash], or __first_node if bucket is null
1678 __node_pointer __pn = __bucket_list_[__chash];
1679 if (__pn == nullptr)
1680 {
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001681 __pn = static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001682 __nd->__next_ = __pn->__next_;
1683 __pn->__next_ = __nd;
1684 // fix up __bucket_list_
1685 __bucket_list_[__chash] = __pn;
1686 if (__nd->__next_ != nullptr)
Howard Hinnant7a445152012-07-06 17:31:14 +00001687 __bucket_list_[__constrain_hash(__nd->__next_->__hash_, __bc)] = __nd;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001688 }
1689 else
1690 {
1691 __nd->__next_ = __pn->__next_;
1692 __pn->__next_ = __nd;
1693 }
1694 __ndptr = __nd;
1695 // increment size
1696 ++size();
1697 __inserted = true;
1698 }
1699__done:
Howard Hinnant39213642013-07-23 22:01:58 +00001700#if _LIBCPP_DEBUG_LEVEL >= 2
1701 return pair<iterator, bool>(iterator(__ndptr, this), __inserted);
1702#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001703 return pair<iterator, bool>(iterator(__ndptr), __inserted);
Howard Hinnant39213642013-07-23 22:01:58 +00001704#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001705}
1706
1707template <class _Tp, class _Hash, class _Equal, class _Alloc>
1708typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1709__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi(__node_pointer __cp)
1710{
1711 __cp->__hash_ = hash_function()(__cp->__value_);
1712 size_type __bc = bucket_count();
1713 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1714 {
Eric Fiselier57947ca2015-02-02 21:31:48 +00001715 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001716 size_type(ceil(float(size() + 1) / max_load_factor()))));
1717 __bc = bucket_count();
1718 }
Howard Hinnant7a445152012-07-06 17:31:14 +00001719 size_t __chash = __constrain_hash(__cp->__hash_, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001720 __node_pointer __pn = __bucket_list_[__chash];
1721 if (__pn == nullptr)
1722 {
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001723 __pn = static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001724 __cp->__next_ = __pn->__next_;
1725 __pn->__next_ = __cp;
1726 // fix up __bucket_list_
1727 __bucket_list_[__chash] = __pn;
1728 if (__cp->__next_ != nullptr)
Howard Hinnant7a445152012-07-06 17:31:14 +00001729 __bucket_list_[__constrain_hash(__cp->__next_->__hash_, __bc)] = __cp;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001730 }
1731 else
1732 {
1733 for (bool __found = false; __pn->__next_ != nullptr &&
Howard Hinnant7a445152012-07-06 17:31:14 +00001734 __constrain_hash(__pn->__next_->__hash_, __bc) == __chash;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001735 __pn = __pn->__next_)
Howard Hinnant324bb032010-08-22 00:02:43 +00001736 {
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001737 // __found key_eq() action
1738 // false false loop
1739 // true true loop
1740 // false true set __found to true
1741 // true false break
1742 if (__found != (__pn->__next_->__hash_ == __cp->__hash_ &&
1743 key_eq()(__pn->__next_->__value_, __cp->__value_)))
1744 {
1745 if (!__found)
1746 __found = true;
1747 else
1748 break;
1749 }
1750 }
1751 __cp->__next_ = __pn->__next_;
1752 __pn->__next_ = __cp;
1753 if (__cp->__next_ != nullptr)
1754 {
Howard Hinnant7a445152012-07-06 17:31:14 +00001755 size_t __nhash = __constrain_hash(__cp->__next_->__hash_, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001756 if (__nhash != __chash)
1757 __bucket_list_[__nhash] = __cp;
1758 }
1759 }
1760 ++size();
Howard Hinnant39213642013-07-23 22:01:58 +00001761#if _LIBCPP_DEBUG_LEVEL >= 2
1762 return iterator(__cp, this);
1763#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001764 return iterator(__cp);
Howard Hinnant39213642013-07-23 22:01:58 +00001765#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001766}
1767
1768template <class _Tp, class _Hash, class _Equal, class _Alloc>
1769typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1770__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi(
1771 const_iterator __p, __node_pointer __cp)
1772{
Howard Hinnant824c1992013-08-02 17:50:49 +00001773#if _LIBCPP_DEBUG_LEVEL >= 2
1774 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1775 "unordered container::emplace_hint(const_iterator, args...) called with an iterator not"
1776 " referring to this unordered container");
1777#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001778 if (__p != end() && key_eq()(*__p, __cp->__value_))
1779 {
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001780 __node_pointer __np = __p.__node_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001781 __cp->__hash_ = __np->__hash_;
1782 size_type __bc = bucket_count();
1783 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1784 {
Eric Fiselier57947ca2015-02-02 21:31:48 +00001785 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001786 size_type(ceil(float(size() + 1) / max_load_factor()))));
1787 __bc = bucket_count();
1788 }
Howard Hinnant7a445152012-07-06 17:31:14 +00001789 size_t __chash = __constrain_hash(__cp->__hash_, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001790 __node_pointer __pp = __bucket_list_[__chash];
1791 while (__pp->__next_ != __np)
1792 __pp = __pp->__next_;
1793 __cp->__next_ = __np;
1794 __pp->__next_ = __cp;
1795 ++size();
Howard Hinnant39213642013-07-23 22:01:58 +00001796#if _LIBCPP_DEBUG_LEVEL >= 2
1797 return iterator(__cp, this);
1798#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001799 return iterator(__cp);
Howard Hinnant39213642013-07-23 22:01:58 +00001800#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001801 }
1802 return __node_insert_multi(__cp);
1803}
1804
1805template <class _Tp, class _Hash, class _Equal, class _Alloc>
1806pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
1807__hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_unique(const value_type& __x)
1808{
Eric Fiselierfdae69a2015-06-13 07:18:32 +00001809 return __insert_unique_value(__x);
1810}
1811
1812
1813#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1814template <class _Tp, class _Hash, class _Equal, class _Alloc>
1815template <class _ValueTp>
1816_LIBCPP_INLINE_VISIBILITY
1817pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
1818__hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_unique_value(_ValueTp&& __x)
1819#else
1820template <class _Tp, class _Hash, class _Equal, class _Alloc>
1821_LIBCPP_INLINE_VISIBILITY
1822pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
1823__hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_unique_value(const value_type& __x)
1824#endif
1825{
1826#if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
1827 typedef const value_type& _ValueTp;
1828#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001829 size_t __hash = hash_function()(__x);
1830 size_type __bc = bucket_count();
1831 bool __inserted = false;
1832 __node_pointer __nd;
1833 size_t __chash;
1834 if (__bc != 0)
1835 {
Howard Hinnant7a445152012-07-06 17:31:14 +00001836 __chash = __constrain_hash(__hash, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001837 __nd = __bucket_list_[__chash];
1838 if (__nd != nullptr)
1839 {
1840 for (__nd = __nd->__next_; __nd != nullptr &&
Howard Hinnant7a445152012-07-06 17:31:14 +00001841 __constrain_hash(__nd->__hash_, __bc) == __chash;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001842 __nd = __nd->__next_)
1843 {
1844 if (key_eq()(__nd->__value_, __x))
1845 goto __done;
1846 }
1847 }
1848 }
1849 {
Duncan P. N. Exon Smith95722352016-01-22 18:27:26 +00001850 __node_holder __h = __construct_node_hash(_VSTD::forward<_ValueTp>(__x), __hash);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001851 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1852 {
Eric Fiselier57947ca2015-02-02 21:31:48 +00001853 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001854 size_type(ceil(float(size() + 1) / max_load_factor()))));
1855 __bc = bucket_count();
Howard Hinnant7a445152012-07-06 17:31:14 +00001856 __chash = __constrain_hash(__hash, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001857 }
1858 // insert_after __bucket_list_[__chash], or __first_node if bucket is null
1859 __node_pointer __pn = __bucket_list_[__chash];
1860 if (__pn == nullptr)
1861 {
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001862 __pn = static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001863 __h->__next_ = __pn->__next_;
1864 __pn->__next_ = __h.get();
1865 // fix up __bucket_list_
1866 __bucket_list_[__chash] = __pn;
1867 if (__h->__next_ != nullptr)
Howard Hinnant7a445152012-07-06 17:31:14 +00001868 __bucket_list_[__constrain_hash(__h->__next_->__hash_, __bc)] = __h.get();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001869 }
1870 else
1871 {
1872 __h->__next_ = __pn->__next_;
1873 __pn->__next_ = __h.get();
1874 }
1875 __nd = __h.release();
1876 // increment size
1877 ++size();
1878 __inserted = true;
1879 }
1880__done:
Howard Hinnant39213642013-07-23 22:01:58 +00001881#if _LIBCPP_DEBUG_LEVEL >= 2
1882 return pair<iterator, bool>(iterator(__nd, this), __inserted);
1883#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001884 return pair<iterator, bool>(iterator(__nd), __inserted);
Howard Hinnant39213642013-07-23 22:01:58 +00001885#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001886}
1887
Howard Hinnant73d21a42010-09-04 23:28:19 +00001888#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1889#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001890
1891template <class _Tp, class _Hash, class _Equal, class _Alloc>
1892template <class... _Args>
1893pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
1894__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique(_Args&&... __args)
1895{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001896 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001897 pair<iterator, bool> __r = __node_insert_unique(__h.get());
1898 if (__r.second)
1899 __h.release();
1900 return __r;
1901}
1902
1903template <class _Tp, class _Hash, class _Equal, class _Alloc>
1904template <class... _Args>
1905typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1906__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_multi(_Args&&... __args)
1907{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001908 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001909 iterator __r = __node_insert_multi(__h.get());
1910 __h.release();
1911 return __r;
1912}
1913
1914template <class _Tp, class _Hash, class _Equal, class _Alloc>
1915template <class... _Args>
1916typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1917__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_hint_multi(
1918 const_iterator __p, _Args&&... __args)
1919{
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00001920#if _LIBCPP_DEBUG_LEVEL >= 2
1921 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1922 "unordered container::emplace_hint(const_iterator, args...) called with an iterator not"
1923 " referring to this unordered container");
1924#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00001925 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001926 iterator __r = __node_insert_multi(__p, __h.get());
1927 __h.release();
1928 return __r;
1929}
1930
Howard Hinnant73d21a42010-09-04 23:28:19 +00001931#endif // _LIBCPP_HAS_NO_VARIADICS
1932
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001933template <class _Tp, class _Hash, class _Equal, class _Alloc>
Eric Fiselierfdae69a2015-06-13 07:18:32 +00001934pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
1935__hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_unique(value_type&& __x)
1936{
1937 return __insert_unique_value(_VSTD::move(__x));
1938}
1939
1940template <class _Tp, class _Hash, class _Equal, class _Alloc>
Howard Hinnant99968442011-11-29 18:15:50 +00001941template <class _Pp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001942pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
Howard Hinnant99968442011-11-29 18:15:50 +00001943__hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_unique(_Pp&& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001944{
Howard Hinnant99968442011-11-29 18:15:50 +00001945 __node_holder __h = __construct_node(_VSTD::forward<_Pp>(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001946 pair<iterator, bool> __r = __node_insert_unique(__h.get());
1947 if (__r.second)
1948 __h.release();
1949 return __r;
1950}
1951
Howard Hinnant73d21a42010-09-04 23:28:19 +00001952#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001953
Howard Hinnant73d21a42010-09-04 23:28:19 +00001954#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001955
1956template <class _Tp, class _Hash, class _Equal, class _Alloc>
Howard Hinnant99968442011-11-29 18:15:50 +00001957template <class _Pp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001958typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
Howard Hinnant99968442011-11-29 18:15:50 +00001959__hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(_Pp&& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001960{
Howard Hinnant99968442011-11-29 18:15:50 +00001961 __node_holder __h = __construct_node(_VSTD::forward<_Pp>(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001962 iterator __r = __node_insert_multi(__h.get());
1963 __h.release();
1964 return __r;
1965}
1966
1967template <class _Tp, class _Hash, class _Equal, class _Alloc>
Howard Hinnant99968442011-11-29 18:15:50 +00001968template <class _Pp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001969typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1970__hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const_iterator __p,
Howard Hinnant99968442011-11-29 18:15:50 +00001971 _Pp&& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001972{
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00001973#if _LIBCPP_DEBUG_LEVEL >= 2
1974 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1975 "unordered container::insert(const_iterator, rvalue) called with an iterator not"
1976 " referring to this unordered container");
1977#endif
Howard Hinnant99968442011-11-29 18:15:50 +00001978 __node_holder __h = __construct_node(_VSTD::forward<_Pp>(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001979 iterator __r = __node_insert_multi(__p, __h.get());
1980 __h.release();
1981 return __r;
1982}
1983
Howard Hinnant73d21a42010-09-04 23:28:19 +00001984#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001985
1986template <class _Tp, class _Hash, class _Equal, class _Alloc>
1987typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1988__hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const value_type& __x)
1989{
1990 __node_holder __h = __construct_node(__x);
1991 iterator __r = __node_insert_multi(__h.get());
1992 __h.release();
1993 return __r;
1994}
1995
1996template <class _Tp, class _Hash, class _Equal, class _Alloc>
1997typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1998__hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const_iterator __p,
1999 const value_type& __x)
2000{
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00002001#if _LIBCPP_DEBUG_LEVEL >= 2
2002 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2003 "unordered container::insert(const_iterator, lvalue) called with an iterator not"
2004 " referring to this unordered container");
2005#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002006 __node_holder __h = __construct_node(__x);
2007 iterator __r = __node_insert_multi(__p, __h.get());
2008 __h.release();
2009 return __r;
2010}
2011
Howard Hinnant73d21a42010-09-04 23:28:19 +00002012#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002013
2014template <class _Tp, class _Hash, class _Equal, class _Alloc>
2015void
2016__hash_table<_Tp, _Hash, _Equal, _Alloc>::rehash(size_type __n)
2017{
Howard Hinnant7a445152012-07-06 17:31:14 +00002018 if (__n == 1)
2019 __n = 2;
2020 else if (__n & (__n - 1))
2021 __n = __next_prime(__n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002022 size_type __bc = bucket_count();
2023 if (__n > __bc)
2024 __rehash(__n);
Howard Hinnant7a445152012-07-06 17:31:14 +00002025 else if (__n < __bc)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002026 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002027 __n = _VSTD::max<size_type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002028 (
2029 __n,
Eric Fiselier57947ca2015-02-02 21:31:48 +00002030 __is_hash_power2(__bc) ? __next_hash_pow2(size_t(ceil(float(size()) / max_load_factor()))) :
2031 __next_prime(size_t(ceil(float(size()) / max_load_factor())))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002032 );
2033 if (__n < __bc)
2034 __rehash(__n);
2035 }
2036}
2037
2038template <class _Tp, class _Hash, class _Equal, class _Alloc>
2039void
2040__hash_table<_Tp, _Hash, _Equal, _Alloc>::__rehash(size_type __nbc)
2041{
Howard Hinnant39213642013-07-23 22:01:58 +00002042#if _LIBCPP_DEBUG_LEVEL >= 2
2043 __get_db()->__invalidate_all(this);
2044#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002045 __pointer_allocator& __npa = __bucket_list_.get_deleter().__alloc();
2046 __bucket_list_.reset(__nbc > 0 ?
2047 __pointer_alloc_traits::allocate(__npa, __nbc) : nullptr);
2048 __bucket_list_.get_deleter().size() = __nbc;
2049 if (__nbc > 0)
2050 {
2051 for (size_type __i = 0; __i < __nbc; ++__i)
2052 __bucket_list_[__i] = nullptr;
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00002053 __node_pointer __pp(static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first())));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002054 __node_pointer __cp = __pp->__next_;
2055 if (__cp != nullptr)
2056 {
Howard Hinnant7a445152012-07-06 17:31:14 +00002057 size_type __chash = __constrain_hash(__cp->__hash_, __nbc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002058 __bucket_list_[__chash] = __pp;
2059 size_type __phash = __chash;
2060 for (__pp = __cp, __cp = __cp->__next_; __cp != nullptr;
2061 __cp = __pp->__next_)
2062 {
Howard Hinnant7a445152012-07-06 17:31:14 +00002063 __chash = __constrain_hash(__cp->__hash_, __nbc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002064 if (__chash == __phash)
2065 __pp = __cp;
2066 else
2067 {
2068 if (__bucket_list_[__chash] == nullptr)
2069 {
2070 __bucket_list_[__chash] = __pp;
2071 __pp = __cp;
2072 __phash = __chash;
2073 }
2074 else
2075 {
2076 __node_pointer __np = __cp;
2077 for (; __np->__next_ != nullptr &&
2078 key_eq()(__cp->__value_, __np->__next_->__value_);
2079 __np = __np->__next_)
2080 ;
2081 __pp->__next_ = __np->__next_;
2082 __np->__next_ = __bucket_list_[__chash]->__next_;
2083 __bucket_list_[__chash]->__next_ = __cp;
Howard Hinnant324bb032010-08-22 00:02:43 +00002084
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002085 }
2086 }
2087 }
2088 }
2089 }
2090}
2091
2092template <class _Tp, class _Hash, class _Equal, class _Alloc>
2093template <class _Key>
2094typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2095__hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k)
2096{
2097 size_t __hash = hash_function()(__k);
2098 size_type __bc = bucket_count();
2099 if (__bc != 0)
2100 {
Howard Hinnant7a445152012-07-06 17:31:14 +00002101 size_t __chash = __constrain_hash(__hash, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002102 __node_pointer __nd = __bucket_list_[__chash];
2103 if (__nd != nullptr)
2104 {
2105 for (__nd = __nd->__next_; __nd != nullptr &&
Howard Hinnant7a445152012-07-06 17:31:14 +00002106 __constrain_hash(__nd->__hash_, __bc) == __chash;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002107 __nd = __nd->__next_)
2108 {
2109 if (key_eq()(__nd->__value_, __k))
Howard Hinnant39213642013-07-23 22:01:58 +00002110#if _LIBCPP_DEBUG_LEVEL >= 2
2111 return iterator(__nd, this);
2112#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002113 return iterator(__nd);
Howard Hinnant39213642013-07-23 22:01:58 +00002114#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002115 }
2116 }
2117 }
2118 return end();
2119}
2120
2121template <class _Tp, class _Hash, class _Equal, class _Alloc>
2122template <class _Key>
2123typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
2124__hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k) const
2125{
2126 size_t __hash = hash_function()(__k);
2127 size_type __bc = bucket_count();
2128 if (__bc != 0)
2129 {
Howard Hinnant7a445152012-07-06 17:31:14 +00002130 size_t __chash = __constrain_hash(__hash, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002131 __node_const_pointer __nd = __bucket_list_[__chash];
2132 if (__nd != nullptr)
2133 {
2134 for (__nd = __nd->__next_; __nd != nullptr &&
Howard Hinnant7a445152012-07-06 17:31:14 +00002135 __constrain_hash(__nd->__hash_, __bc) == __chash;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002136 __nd = __nd->__next_)
2137 {
2138 if (key_eq()(__nd->__value_, __k))
Howard Hinnant39213642013-07-23 22:01:58 +00002139#if _LIBCPP_DEBUG_LEVEL >= 2
2140 return const_iterator(__nd, this);
2141#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002142 return const_iterator(__nd);
Howard Hinnant39213642013-07-23 22:01:58 +00002143#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002144 }
2145 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002146
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002147 }
2148 return end();
2149}
2150
Howard Hinnant73d21a42010-09-04 23:28:19 +00002151#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2152#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002153
2154template <class _Tp, class _Hash, class _Equal, class _Alloc>
2155template <class ..._Args>
2156typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
2157__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(_Args&& ...__args)
2158{
2159 __node_allocator& __na = __node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00002160 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Howard Hinnant0949eed2011-06-30 21:18:19 +00002161 __node_traits::construct(__na, _VSTD::addressof(__h->__value_), _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002162 __h.get_deleter().__value_constructed = true;
2163 __h->__hash_ = hash_function()(__h->__value_);
2164 __h->__next_ = nullptr;
2165 return __h;
2166}
2167
Howard Hinnant73d21a42010-09-04 23:28:19 +00002168#endif // _LIBCPP_HAS_NO_VARIADICS
2169
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002170template <class _Tp, class _Hash, class _Equal, class _Alloc>
Duncan P. N. Exon Smith95722352016-01-22 18:27:26 +00002171template <class _ValueTp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002172typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Duncan P. N. Exon Smith95722352016-01-22 18:27:26 +00002173__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node_hash(_ValueTp&& __v,
2174 size_t __hash)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002175{
2176 __node_allocator& __na = __node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00002177 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Duncan P. N. Exon Smith95722352016-01-22 18:27:26 +00002178 __node_traits::construct(__na, _VSTD::addressof(__h->__value_), _VSTD::forward<_ValueTp>(__v));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002179 __h.get_deleter().__value_constructed = true;
2180 __h->__hash_ = __hash;
2181 __h->__next_ = nullptr;
Howard Hinnant9a894d92013-08-22 18:29:50 +00002182 return __h;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002183}
2184
Howard Hinnant73d21a42010-09-04 23:28:19 +00002185#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002186
2187template <class _Tp, class _Hash, class _Equal, class _Alloc>
2188typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
2189__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(const value_type& __v)
2190{
2191 __node_allocator& __na = __node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00002192 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Howard Hinnant0949eed2011-06-30 21:18:19 +00002193 __node_traits::construct(__na, _VSTD::addressof(__h->__value_), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002194 __h.get_deleter().__value_constructed = true;
2195 __h->__hash_ = hash_function()(__h->__value_);
2196 __h->__next_ = nullptr;
Dimitry Andric89663502015-08-19 06:43:33 +00002197 return _LIBCPP_EXPLICIT_MOVE(__h); // explicitly moved for C++03
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002198}
2199
Howard Hinnant73d21a42010-09-04 23:28:19 +00002200#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002201
2202template <class _Tp, class _Hash, class _Equal, class _Alloc>
2203typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Duncan P. N. Exon Smith95722352016-01-22 18:27:26 +00002204__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node_hash(const value_type& __v,
2205 size_t __hash)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002206{
2207 __node_allocator& __na = __node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00002208 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Howard Hinnant0949eed2011-06-30 21:18:19 +00002209 __node_traits::construct(__na, _VSTD::addressof(__h->__value_), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002210 __h.get_deleter().__value_constructed = true;
2211 __h->__hash_ = __hash;
2212 __h->__next_ = nullptr;
Dimitry Andric89663502015-08-19 06:43:33 +00002213 return _LIBCPP_EXPLICIT_MOVE(__h); // explicitly moved for C++03
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002214}
2215
2216template <class _Tp, class _Hash, class _Equal, class _Alloc>
2217typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2218__hash_table<_Tp, _Hash, _Equal, _Alloc>::erase(const_iterator __p)
2219{
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00002220 __node_pointer __np = __p.__node_;
Howard Hinnant39213642013-07-23 22:01:58 +00002221#if _LIBCPP_DEBUG_LEVEL >= 2
2222 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2223 "unordered container erase(iterator) called with an iterator not"
2224 " referring to this container");
2225 _LIBCPP_ASSERT(__p != end(),
2226 "unordered container erase(iterator) called with a non-dereferenceable iterator");
2227 iterator __r(__np, this);
2228#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002229 iterator __r(__np);
Howard Hinnant39213642013-07-23 22:01:58 +00002230#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002231 ++__r;
2232 remove(__p);
2233 return __r;
2234}
2235
2236template <class _Tp, class _Hash, class _Equal, class _Alloc>
2237typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2238__hash_table<_Tp, _Hash, _Equal, _Alloc>::erase(const_iterator __first,
2239 const_iterator __last)
2240{
Howard Hinnant39213642013-07-23 22:01:58 +00002241#if _LIBCPP_DEBUG_LEVEL >= 2
2242 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
2243 "unodered container::erase(iterator, iterator) called with an iterator not"
2244 " referring to this unodered container");
2245 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__last) == this,
2246 "unodered container::erase(iterator, iterator) called with an iterator not"
2247 " referring to this unodered container");
2248#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002249 for (const_iterator __p = __first; __first != __last; __p = __first)
2250 {
2251 ++__first;
2252 erase(__p);
2253 }
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00002254 __node_pointer __np = __last.__node_;
Howard Hinnant39213642013-07-23 22:01:58 +00002255#if _LIBCPP_DEBUG_LEVEL >= 2
2256 return iterator (__np, this);
2257#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002258 return iterator (__np);
Howard Hinnant39213642013-07-23 22:01:58 +00002259#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002260}
2261
2262template <class _Tp, class _Hash, class _Equal, class _Alloc>
2263template <class _Key>
2264typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2265__hash_table<_Tp, _Hash, _Equal, _Alloc>::__erase_unique(const _Key& __k)
2266{
2267 iterator __i = find(__k);
2268 if (__i == end())
2269 return 0;
2270 erase(__i);
2271 return 1;
2272}
2273
2274template <class _Tp, class _Hash, class _Equal, class _Alloc>
2275template <class _Key>
2276typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2277__hash_table<_Tp, _Hash, _Equal, _Alloc>::__erase_multi(const _Key& __k)
2278{
2279 size_type __r = 0;
2280 iterator __i = find(__k);
2281 if (__i != end())
2282 {
2283 iterator __e = end();
2284 do
2285 {
2286 erase(__i++);
2287 ++__r;
2288 } while (__i != __e && key_eq()(*__i, __k));
2289 }
2290 return __r;
2291}
2292
2293template <class _Tp, class _Hash, class _Equal, class _Alloc>
2294typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00002295__hash_table<_Tp, _Hash, _Equal, _Alloc>::remove(const_iterator __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002296{
2297 // current node
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00002298 __node_pointer __cn = __p.__node_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002299 size_type __bc = bucket_count();
Howard Hinnant7a445152012-07-06 17:31:14 +00002300 size_t __chash = __constrain_hash(__cn->__hash_, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002301 // find previous node
2302 __node_pointer __pn = __bucket_list_[__chash];
2303 for (; __pn->__next_ != __cn; __pn = __pn->__next_)
2304 ;
2305 // Fix up __bucket_list_
2306 // if __pn is not in same bucket (before begin is not in same bucket) &&
2307 // if __cn->__next_ is not in same bucket (nullptr is not in same bucket)
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00002308 if (__pn == static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()))
2309 || __constrain_hash(__pn->__hash_, __bc) != __chash)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002310 {
Howard Hinnant7a445152012-07-06 17:31:14 +00002311 if (__cn->__next_ == nullptr || __constrain_hash(__cn->__next_->__hash_, __bc) != __chash)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002312 __bucket_list_[__chash] = nullptr;
2313 }
2314 // if __cn->__next_ is not in same bucket (nullptr is in same bucket)
2315 if (__cn->__next_ != nullptr)
2316 {
Howard Hinnant7a445152012-07-06 17:31:14 +00002317 size_t __nhash = __constrain_hash(__cn->__next_->__hash_, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002318 if (__nhash != __chash)
2319 __bucket_list_[__nhash] = __pn;
2320 }
2321 // remove __cn
2322 __pn->__next_ = __cn->__next_;
2323 __cn->__next_ = nullptr;
2324 --size();
Howard Hinnant39213642013-07-23 22:01:58 +00002325#if _LIBCPP_DEBUG_LEVEL >= 2
2326 __c_node* __c = __get_db()->__find_c_and_lock(this);
2327 for (__i_node** __p = __c->end_; __p != __c->beg_; )
2328 {
2329 --__p;
2330 iterator* __i = static_cast<iterator*>((*__p)->__i_);
2331 if (__i->__node_ == __cn)
2332 {
2333 (*__p)->__c_ = nullptr;
2334 if (--__c->end_ != __p)
2335 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
2336 }
2337 }
2338 __get_db()->unlock();
2339#endif
Howard Hinnant99968442011-11-29 18:15:50 +00002340 return __node_holder(__cn, _Dp(__node_alloc(), true));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002341}
2342
2343template <class _Tp, class _Hash, class _Equal, class _Alloc>
2344template <class _Key>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00002345inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002346typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2347__hash_table<_Tp, _Hash, _Equal, _Alloc>::__count_unique(const _Key& __k) const
2348{
2349 return static_cast<size_type>(find(__k) != end());
2350}
2351
2352template <class _Tp, class _Hash, class _Equal, class _Alloc>
2353template <class _Key>
2354typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2355__hash_table<_Tp, _Hash, _Equal, _Alloc>::__count_multi(const _Key& __k) const
2356{
2357 size_type __r = 0;
2358 const_iterator __i = find(__k);
2359 if (__i != end())
2360 {
2361 const_iterator __e = end();
2362 do
2363 {
2364 ++__i;
2365 ++__r;
2366 } while (__i != __e && key_eq()(*__i, __k));
2367 }
2368 return __r;
2369}
2370
2371template <class _Tp, class _Hash, class _Equal, class _Alloc>
2372template <class _Key>
2373pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator,
2374 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator>
2375__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_unique(
2376 const _Key& __k)
2377{
2378 iterator __i = find(__k);
2379 iterator __j = __i;
2380 if (__i != end())
2381 ++__j;
2382 return pair<iterator, iterator>(__i, __j);
2383}
2384
2385template <class _Tp, class _Hash, class _Equal, class _Alloc>
2386template <class _Key>
2387pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator,
2388 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator>
2389__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_unique(
2390 const _Key& __k) const
2391{
2392 const_iterator __i = find(__k);
2393 const_iterator __j = __i;
2394 if (__i != end())
2395 ++__j;
2396 return pair<const_iterator, const_iterator>(__i, __j);
2397}
2398
2399template <class _Tp, class _Hash, class _Equal, class _Alloc>
2400template <class _Key>
2401pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator,
2402 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator>
2403__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_multi(
2404 const _Key& __k)
2405{
2406 iterator __i = find(__k);
2407 iterator __j = __i;
2408 if (__i != end())
2409 {
2410 iterator __e = end();
2411 do
2412 {
2413 ++__j;
2414 } while (__j != __e && key_eq()(*__j, __k));
2415 }
2416 return pair<iterator, iterator>(__i, __j);
2417}
2418
2419template <class _Tp, class _Hash, class _Equal, class _Alloc>
2420template <class _Key>
2421pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator,
2422 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator>
2423__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_multi(
2424 const _Key& __k) const
2425{
2426 const_iterator __i = find(__k);
2427 const_iterator __j = __i;
2428 if (__i != end())
2429 {
2430 const_iterator __e = end();
2431 do
2432 {
2433 ++__j;
2434 } while (__j != __e && key_eq()(*__j, __k));
2435 }
2436 return pair<const_iterator, const_iterator>(__i, __j);
2437}
2438
2439template <class _Tp, class _Hash, class _Equal, class _Alloc>
2440void
2441__hash_table<_Tp, _Hash, _Equal, _Alloc>::swap(__hash_table& __u)
Eric Fiselier692177d2015-07-18 20:40:46 +00002442#if _LIBCPP_STD_VER <= 11
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00002443 _NOEXCEPT_(
Marshall Clow7d914d12015-07-13 20:04:56 +00002444 __is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value
Marshall Clow7d914d12015-07-13 20:04:56 +00002445 && (!allocator_traits<__pointer_allocator>::propagate_on_container_swap::value
2446 || __is_nothrow_swappable<__pointer_allocator>::value)
2447 && (!__node_traits::propagate_on_container_swap::value
2448 || __is_nothrow_swappable<__node_allocator>::value)
Marshall Clow7d914d12015-07-13 20:04:56 +00002449 )
Eric Fiselier692177d2015-07-18 20:40:46 +00002450#else
2451 _NOEXCEPT_(__is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value)
2452#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002453{
2454 {
2455 __node_pointer_pointer __npp = __bucket_list_.release();
2456 __bucket_list_.reset(__u.__bucket_list_.release());
2457 __u.__bucket_list_.reset(__npp);
2458 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002459 _VSTD::swap(__bucket_list_.get_deleter().size(), __u.__bucket_list_.get_deleter().size());
Marshall Clow7d914d12015-07-13 20:04:56 +00002460 __swap_allocator(__bucket_list_.get_deleter().__alloc(),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002461 __u.__bucket_list_.get_deleter().__alloc());
Marshall Clow7d914d12015-07-13 20:04:56 +00002462 __swap_allocator(__node_alloc(), __u.__node_alloc());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002463 _VSTD::swap(__p1_.first().__next_, __u.__p1_.first().__next_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002464 __p2_.swap(__u.__p2_);
2465 __p3_.swap(__u.__p3_);
2466 if (size() > 0)
Howard Hinnant7a445152012-07-06 17:31:14 +00002467 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash_, bucket_count())] =
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00002468 static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002469 if (__u.size() > 0)
Howard Hinnant7a445152012-07-06 17:31:14 +00002470 __u.__bucket_list_[__constrain_hash(__u.__p1_.first().__next_->__hash_, __u.bucket_count())] =
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00002471 static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__u.__p1_.first()));
Howard Hinnant39213642013-07-23 22:01:58 +00002472#if _LIBCPP_DEBUG_LEVEL >= 2
2473 __get_db()->swap(this, &__u);
2474#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002475}
2476
2477template <class _Tp, class _Hash, class _Equal, class _Alloc>
2478typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2479__hash_table<_Tp, _Hash, _Equal, _Alloc>::bucket_size(size_type __n) const
2480{
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00002481 _LIBCPP_ASSERT(__n < bucket_count(),
2482 "unordered container::bucket_size(n) called with n >= bucket_count()");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002483 __node_const_pointer __np = __bucket_list_[__n];
2484 size_type __bc = bucket_count();
2485 size_type __r = 0;
2486 if (__np != nullptr)
2487 {
2488 for (__np = __np->__next_; __np != nullptr &&
Howard Hinnant7a445152012-07-06 17:31:14 +00002489 __constrain_hash(__np->__hash_, __bc) == __n;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002490 __np = __np->__next_, ++__r)
2491 ;
2492 }
2493 return __r;
2494}
2495
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00002496template <class _Tp, class _Hash, class _Equal, class _Alloc>
2497inline _LIBCPP_INLINE_VISIBILITY
2498void
2499swap(__hash_table<_Tp, _Hash, _Equal, _Alloc>& __x,
2500 __hash_table<_Tp, _Hash, _Equal, _Alloc>& __y)
2501 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
2502{
2503 __x.swap(__y);
2504}
2505
Howard Hinnant39213642013-07-23 22:01:58 +00002506#if _LIBCPP_DEBUG_LEVEL >= 2
2507
2508template <class _Tp, class _Hash, class _Equal, class _Alloc>
2509bool
2510__hash_table<_Tp, _Hash, _Equal, _Alloc>::__dereferenceable(const const_iterator* __i) const
2511{
2512 return __i->__node_ != nullptr;
2513}
2514
2515template <class _Tp, class _Hash, class _Equal, class _Alloc>
2516bool
2517__hash_table<_Tp, _Hash, _Equal, _Alloc>::__decrementable(const const_iterator*) const
2518{
2519 return false;
2520}
2521
2522template <class _Tp, class _Hash, class _Equal, class _Alloc>
2523bool
2524__hash_table<_Tp, _Hash, _Equal, _Alloc>::__addable(const const_iterator*, ptrdiff_t) const
2525{
2526 return false;
2527}
2528
2529template <class _Tp, class _Hash, class _Equal, class _Alloc>
2530bool
2531__hash_table<_Tp, _Hash, _Equal, _Alloc>::__subscriptable(const const_iterator*, ptrdiff_t) const
2532{
2533 return false;
2534}
2535
2536#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002537_LIBCPP_END_NAMESPACE_STD
2538
2539#endif // _LIBCPP__HASH_TABLE